p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Cannot upload files Help!!!!


Message #1 by "Diptanjan Mukherjee" <diptanjan@h...> on Mon, 16 Sep 2002 00:12:56 +0530
Hello friends ,
I am new to PHP, I am studying the PHP4 book to learn the subject. But I am
having some problem to run an example on Lesson 10 (File and Directory
Handling) of this book . I want to upload a file using PHP. Here I am trying
upload all the files in a folder named -"filestore" which is on d: drive.
When ever I am trying to upload the files by clicking the submit button
nothing happens only the page is refreshed. NO error messages or  nothing
else.!!!
Where am I going wrong??????? I should mention here that I am working on Win
2000 platform and have iis installed. I also run ASP scripts from this iis
and which is running absolotuly fine. Am I making any coading mistake?

PLEASE HELP
Diptanjan

I am providing all the codes here :

<?
$archive_dir="/filestore";
function upload_form()
{
    global $PHP_SELF;
?>
<form method="post" ENCTYPE="MULTIPART/FORM-DATA" ACTION="<? echo $PHP_SELF
?>">
<input type="hidden" name="action" value="upload">
Upload file!
<input type="file" name="userfile">
<input type="submit" name="SUBMIT" value="upload">
</form>
<?
}

function upload_file()
{
global $userfile, $userfile_name, $userfile_size, $userfile_type,
$archive_dir, $WINDIR;

if(isset($WINDIR)) $userfile=str_replace("\\\\","\\",$userfile);
$filename=basename($userfile_name);

if($userfile_size<=0) die ("$filename is empty.");

if(!@copy($userfile, "$archive_dir/$userfile"))
die("Cant copy $userfile_name to $filename.");

if(isset($WINDIR) && !@unlink($userfile))
die("cant delete the file $userfile_name.");

echo "$filename has been successfully uploaded<br>";
echo "Filesize: ".number_format($userfile_size)."<br>";
echo "FIletype: $userfile_type<br>";

}
?>
<html>
<head><title>File Uploadf</title></head>
<body>
<?
if(action=='upload') upload_file();
else upload_form();
?>
</body>
</html>




Message #2 by "Chuckster" <bnm@c...> on Mon, 16 Sep 2002 02:10:41
I don't know anything about iis but you might enter the 'full' path to the 
directory at the top ie:

$archive_dir="C:\filestore";

and I 'think' you use backslashes in windows, not sure. Also you have to 
have the folder chmod to 777 to upload to it.

Hope this helps
I'm sure Nik will help if it doesn't

Chuckster <><
Message #3 by "UseLess, B" <UseLess@n...> on Mon, 16 Sep 2002 13:25:26 +0100
Greetings,

You need to make sure that the account 'IUSR_<machinename>' has at least
add/read access to the folder you wish to upload files to, if you intend to
allow files to be deleted (i.e. same filename etc) then you should allow
change access.

Also you need to use 2 backslashes within your script for paths in Windows;

i.e. $archive_dir="\\filestore\\";

Regards

Barry.
Message #4 by "Diptanjan Mukherjee" <diptanjan@h...> on Tue, 17 Sep 2002 01:12:16 +0530
 Hello friends ,
I am still without luck!!
Where am I going wrong??????? Even if I press submit without providing any
file name then too its not showing any error where as according to my codes
there should be a message saying  "filename is empty" . I tried to upload
files using ASP scripts and its running absolotuly fine.
Am I making any coading mistake? I Have my codes below if anybody try my
codes in win 2000 platform
and it runs I will be sure that there is nothing wrong with my codes.

 PLEASE HELP
 Diptanjan

 I am providing all the codes here :

 <?
 $archive_dir="/filestore";
 function upload_form()
 {
     global $PHP_SELF;
 ?>
 <form method="post" ENCTYPE="MULTIPART/FORM-DATA" ACTION="<? echo $PHP_SELF
 ?>">
 <input type="hidden" name="action" value="upload">
 Upload file!
 <input type="file" name="userfile">
 <input type="submit" name="SUBMIT" value="upload">
 </form>
 <?
 }

 function upload_file()
 {
 global $userfile, $userfile_name, $userfile_size, $userfile_type,
 $archive_dir, $WINDIR;

 if(isset($WINDIR)) $userfile=str_replace("\\\\","\\",$userfile);
 $filename=basename($userfile_name);

 if($userfile_size<=0) die ("$filename is empty.");

 if(!@copy($userfile, "$archive_dir/$userfile"))
 die("Cant copy $userfile_name to $filename.");

 if(isset($WINDIR) && !@unlink($userfile))
 die("cant delete the file $userfile_name.");

 echo "$filename has been successfully uploaded<br>";
 echo "Filesize: ".number_format($userfile_size)."<br>";
 echo "FIletype: $userfile_type<br>";

 }
 ?>
 <html>
 <head><title>File Uploadf</title></head>
 <body>
 <?
 if(action=='upload')
upload_file();
 else upload_form();
 ?>
 </body>
 </html>


  Return to Index