Jump to content

single file upload page


Maxraid

Recommended Posts

I took this code straight from a wrox book i got, but I cant make it work  :-\. It seems like it keeps running the the upload_form insted of the upload_file function  ???
[code]
<?
//file_upload.php
$archive_dir = "./docs";
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/$filename"))
      die("Can't copy $userfile_name to $filename.");
 
  if(!isset($WINDIR) && !@unlink($userfile))
      die ("Can't 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 UPLOAD</TITLE></HEAD>
<BODY>
<?
if($action == 'upload') upload_file();
else upload_form();
?>
</BODY>
</HTML>
[/code]
Can anybody see whats wrong, I tried it both on my own server and  on a hosted server
Link to comment
https://forums.phpfreaks.com/topic/28834-single-file-upload-page/
Share on other sites

The book you have is out of date. Try...

[code=php:0]
if (isset($_POST['action'])) {
  upload_file();
} else {
  upload_form();
}
[/code]

Also, you do not need the line [i]global $PHP_SELF;[/i] within upload_form(), never did, not sure why that would be there.

ACTION="<? echo $PHP_SELF ?>"> should be ACTION="<?php echo $_SERVER['PHP_SELF'] ?>">

And all instances of <? should be <?php
It helped a littlebit, now its running until the line where the script is checking if the filesize is empty, then its just giving me the message "is empty".
So i went to wrox website and downloaded the code for the book "beginning php5".
I can't see much difference?
8)
[code]
<?
//file_upload.php
$archive_dir = "/Inetpub/wwwroot/beginning_php5/ch07/docs";
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/$filename"))
      die("Can't copy $userfile_name to $filename.");
 
  if(isset($WINDIR) && !@unlink($userfile))
      die ("Can't 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 upload</title></head>
<body>
<?
if($action == 'upload') upload_file();

else upload_form();
?>
</body>
</html>
[/code]
That looks to me like the code is reused ???
I went to the phpfreaks code library an picked up this code and its working perfectly [url=http://www.phpfreaks.com/quickcode/Upload-Multiple-Files-Simple-Code/582.php]http://www.phpfreaks.com/quickcode/Upload-Multiple-Files-Simple-Code/582.php[/url]
But I stil don't understand about wrox's code, because i was thinking to buy that php5 book from them but now I have second thoughts :-\

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.