troubleshooting Question

PHP UPLOAD ERROR...

Avatar of cgustaf
cgustaf asked on
PHP
28 Comments1 Solution550 ViewsLast Modified:
I am trying to use the following script, but it produces a persistent error (as noted below the script.  Maybe the issue is naming a temp directory, but I am unable to determine this.  Here is the scrpt and the error:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
 
<body>
 <?php // RAY_upload_example.php
error_reporting(E_ALL);
 
// MANUAL REFERENCE PAGES
// http://docs.php.net/manual/en/features.file-upload.php
// http://docs.php.net/manual/en/features.file-upload.common-pitfalls.php
// http://docs.php.net/manual/en/function.move-uploaded-file.php
// http://docs.php.net/manual/en/function.getimagesize.php
 
// ESTABLISH THE NAME OF THE 'uploads' DIRECTORY
$uploads = '/upload';
 
// ESTABLISH THE BIGGEST FILE SIZE WE CAN ACCEPT
$max_file_size = '8192000';  // EIGHT MEGABYTE LIMIT ON UPLOADS
 
// ESTABLISH THE KINDS OF FILE EXTENSIONS WE CAN ACCEPT
// $file_exts = array('jpg', 'gif', 'png', 'txt');
$file_exts = array('rtf');
 
// ESTABLISH THE NUMBER OF FILES WE CAN UPLOAD
// $nf = 3;
$nf = 1; 
 
 
// THIS IS A LIST OF THE POSSIBLE ERRORS THAT CAN BE REPORTED IN $_FILES[]["error"]
$errors&#9;= array(
&#9;0 => "Success!",
&#9;1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini",
&#9;2 => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
&#9;3 => "The uploaded file was only partially uploaded",
&#9;4 => "No file was uploaded",
&#9;6 => "Missing a temporary folder",
&#9;7 => "Cannot write file to disk"
);
 
 
 
// IF THERE IS NOTHING IN $_POST, PUT UP THE FORM FOR INPUT
if (empty($_POST))
{
&#9;?>
&#9;<h2>Upload <?=$nf?> file(s)</h2>
 
&#9;<!--
&#9;&#9;SOME THINGS TO NOTE ABOUT THIS FORM...
&#9;&#9;NOTE THE CHOICE OF ENCTYPE IN THE HTML FORM STATEMENT
&#9;&#9;MAX_FILE_SIZE MUST PRECEDE THE FILE INPUT FIELD
&#9;&#9;INPUT NAME= IN TYPE=FILE DETERMINES THE NAME YOU FIND IN $_FILES ARRAY
&#9;-->
 
    <form name="UploadForm" enctype="multipart/form-data" action="<?=$_SERVER["../../../REQUEST_URI"]?>" method="POST">
&#9;<input type="hidden" name="p" value="1" />
&#9;<input type="hidden" name="MAX_FILE_SIZE" value="<?=$max_file_size?>" />
&#9;<p>
&#9;Find the file(s) you want to upload and click the "Upload" button below.
&#9;</p>
 
&#9;<?php for ($n = 0; $n < $nf; $n++)
&#9;&#9;{
&#9;&#9;&#9;echo "<input name=\"userfile$n\" type=\"file\" size=\"80\" /><br/>\n";
&#9;&#9;}
&#9;?>
&#9;<br/>Check this box <input autocomplete="off" type="checkbox" name="overwrite" /> to <b>overwrite</b> existing files.
&#9;<input type="submit" name="_submit" value="Upload" />
&#9;</form>
&#9;<?php
&#9;die();
}
 
 
 
else // WE HAVE GOT SOMETHING IN $_POST
{
 
// THERE IS POST DATA - PROCESS IT
&#9;echo "<h2>Results: File Upload</h2>\n";
 
// ACTIVATE THIS TO SEE WHAT IS COMING THROUGH
echo "<pre>"; var_dump($_FILES); var_dump($_POST); echo "</pre>\n";
 
// ITERATE OVER THE CONTENTS OF $_FILES
&#9;foreach ($_FILES as $my_uploaded_file)
&#9;{
 
// SKIP OVER EMPTY SPOTS - NOTHING UPLOADED
&#9;&#9;$error_code&#9;= $my_uploaded_file["error"];
&#9;&#9;if ($error_code == 4) continue;
 
// SYNTHESIZE THE NEW FILE NAME
&#9;&#9;$f_type&#9;= trim(strtolower(end    (explode( '.', basename($my_uploaded_file['name'] )))));
&#9;&#9;$f_name&#9;= trim(strtolower(current(explode( '.', basename($my_uploaded_file['name'] )))));
&#9;&#9;$my_new_file = getcwd() . '/' . $uploads . '/' . $f_name .'.'. $f_type;
&#9;&#9;$my_file     = $uploads . '/' . $f_name .'.'. $f_type;
 
// OPTIONAL TEST FOR ALLOWABLE EXTENSIONS
&#9;&#9;if (!in_array($f_type, $file_exts)) die("Sorry, $f_type files not allowed");
 
// IF THERE ARE ERRORS
&#9;&#9;if ($error_code != 0)
&#9;&#9;{
&#9;&#9;&#9;$error_message = $errors[$error_code];
&#9;&#9;&#9;die("Sorry, Upload Error Code: $error_code: $error_message");
&#9;&#9;}
 
// GET THE FILE SIZE
&#9;&#9;$file_size&#9;= number_format($my_uploaded_file["size"]);
 
// MOVE THE FILE INTO THE DIRECTORY
// IF THE FILE IS NEW
&#9;&#9;if (!file_exists($my_new_file))
&#9;&#9;{
&#9;&#9;&#9;if (move_uploaded_file($my_uploaded_file['tmp_name'], $my_new_file))
&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;$upload_success = 1;
&#9;&#9;&#9;}
&#9;&#9;&#9;else
&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;$upload_success = -1;
&#9;&#9;&#9;}
 
// IF THE FILE ALREADY EXISTS
&#9;&#9;}
&#9;&#9;else
&#9;&#9;{
&#9;&#9;&#9;echo "<br/><b><i>$my_file</i></b> already exists.\n";

 
// SHOULD WE OVERWRITE THE FILE? IF NOT
&#9;&#9;&#9;if (empty($_POST["overwrite"]))
&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;$upload_success = 0;
 
// IF WE SHOULD OVERWRITE THE FILE, TRY TO MAKE A BACKUP
&#9;&#9;&#9;}
&#9;&#9;&#9;else
&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;$now&#9;= date('Y-m-d');
&#9;&#9;&#9;&#9;$my_bak = $my_new_file . '.' . $now . '.bak';
&#9;&#9;&#9;&#9;if (!copy($my_new_file, $my_bak))
&#9;&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;&#9;echo "<br/><b>Attempted Backup Failed!</b>\n";
&#9;&#9;&#9;&#9;}
&#9;&#9;&#9;&#9;if (move_uploaded_file($my_uploaded_file['tmp_name'], $my_new_file))
&#9;&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;&#9;$upload_success = 2;
&#9;&#9;&#9;&#9;}
&#9;&#9;&#9;&#9;else
&#9;&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;&#9;$upload_success = -1;
&#9;&#9;&#9;&#9;}
&#9;&#9;&#9;}
&#9;&#9;}
 
// REPORT OUR SUCCESS OR FAILURE
&#9;&#9;if ($upload_success == 2) { echo "<br/>It has been overwritten.\n"; }
&#9;&#9;if ($upload_success == 1) { echo "<br/><b><i>$my_file</i></b> has been saved.\n"; }
&#9;&#9;if ($upload_success == 0) { echo "<br/><b>It was NOT overwritten.</b>\n"; }
&#9;&#9;if ($upload_success < 0)  { echo "<br/><b>ERROR <i>$my_file</i> NOT SAVED - SEE WARNING FROM move_uploaded_file() COMMAND</b>\n"; }
&#9;&#9;if ($upload_success > 0)
&#9;&#9;{
&#9;&#9;&#9;echo "$file_size bytes uploaded.\n";
&#9;&#9;&#9;if (!chmod ($my_new_file, 0755))
&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;echo "<br/>chmod(0755) FAILED: fileperms() = ";
&#9;&#9;&#9;&#9;echo substr(sprintf('%o', fileperms($my_new_file)), -4);
&#9;&#9;&#9;}
&#9;&#9;&#9;echo "<br/><a href=\"$my_file\">See the file $my_file</a>\n";
&#9;&#9;}
// END ITERATOR
&#9;}
}
?> 
 
</body>
</html>


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
RUNNING THE ABOVE CODE PRODUCES THE FOLLOWING ERROR.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Results: File Upload
 array(1) {
  ["userfile0"]=>
  array(5) {
    ["name"]=>
    string(6) "HI.rtf"
    ["type"]=>
    string(18) "application/msword"
    ["tmp_name"]=>
    string(37) "/services/webdata/phpupload/php6UMJgQ"
    ["error"]=>
    int(0)
    ["size"]=>
    int(4621)
  }
}
array(3) {
  ["p"]=>
  string(1) "1"
  ["MAX_FILE_SIZE"]=>
  string(7) "8192000"
  ["_submit"]=>
  string(6) "Upload"
}

Open in new window


ERROR /upload/hi.rtf NOT SAVED - SEE WARNING FROM move_uploaded_file() COMMAND
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 28 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 28 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros