4 Replies - 2520 Views - Last Post: 05 November 2011 - 09:58 AM Rate Topic: -----

#1 kurazi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 27-March 11

single file upload form

Posted 01 November 2011 - 05:48 PM

I am new to php, and I am having trouble with a simple file(i followed a tutorial, but there was no other help files) that allows a person to upload a txt file to a folder on their server. For some reason when i test it and click upload, it hangs for a moment, and i get an error, "sorry your file could not be uploaded"... which is what i have it coded to do if there is an error. I have tried running it on 2 different servers, to which i get the same error. Not sure if there is anything I am missing. I have permission on the folder set to allow everything.

Here is the code for the form
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />  
 <title>PHP Upload System</title>  
 </head>  
 <body>  
 <form name="file_uploader" action="upload_ac.php" method="post" enctype="multipart/form-data">  
 <input type="file" name="data"><br />  
 <input type="submit" name="upload" value="Upload File">  
 </form>  
 </body>  
 </html> 



<?php  

 # Check to see if the file is accessible  
 if ( !isset($_FILES['data']['name']) || $_FILES['data']['name'] == '' ) {  
 	die('No input file specified. Please go back and select a file to upload.');  
 } // End check for file being set  
    
$max_filesize = 999999;  
$filetype = 'text/plain';  
$upload_path = '/upload/';  

 # Check to see if the filesize is too large  
 if ($_FILES['data']['size'] > $max_filesize) {  
 	die('Your filesize is too large. Please make your filesize smaller than ' . $max_filesize . ' bytes.');  
 } // End if filesize is too large   

 # Check to see if the filetype is correct  
 if ($_FILES['data']['type'] != $filetype) {  
 	die('Sorry, your file was not of the ' . $filetype . ' mimetype (yours was ' . $_FILES['data']['type'] . ').');  
 } // End filetype check  

 # If file has gotten this far, it is successful  
 $copy_to = $_SERVER['DOCUMENT_ROOT'] . $upload_path . $_FILES['data']['name'];   

 # Upload the file  
$upload = move_uploaded_file($_FILES['data']['tmp_name'], $copy_to);  

 # Check to see if upload was successful  
 if (!$upload) {  
 die('Sorry, your file could not be uploaded.');  
}    
echo 'Your file contents are below: <hr>' . file_get_contents($copy_to);  
?> 



Is This A Good Question/Topic? 0
  • +

Replies To: single file upload form

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: single file upload form

Posted 01 November 2011 - 06:43 PM

Check to see if the $copy_to value is as you expect it to be. The var_dump is handy for stuff like that.

Also, when the move_uploaded_file function fails, there is often a warning shown to explain why. If there is it might explain why it's failing. Add this to the top of the page to show it: (That is if you don't have error reporting enabled already.)
ini_set('display_errors', true);
error_reporting(E_ALL);



You should also be checking to see if the $_FILES['data']['error'] element is set. That's what PHP uses to report it when file uploads fail. See Error Messages Explained in the manual for the meaning of the error codes.
Was This Post Helpful? 0
  • +
  • -

#3 kurazi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 27-March 11

Re: single file upload form

Posted 02 November 2011 - 06:55 PM

Hey Thank for your help. I added that bit of code into my file and I am now getting the following error

Warning: move_uploaded_file() [function.move-uploaded-file]: open_basedir restriction in effect. File(/var/www/vhosts/XXX.ca/httpdocs/upload/help.txt) is not within the allowed path(s): (/var/www/vhosts/XXX.ca/httpdocs:/tmp) in /home/vhosts/XXX.ca/httpdocs/upload_ac.php on line 33

Its on this line : $upload = move_uploaded_file($_FILES['data']['tmp_name'], $copy_to);

I wonder, is it having trouble putting the file in a temp directory - XXX.ca/httpdocs:/tmp
Maybe I have to create the folder /tmp?

Any suggestions?

View PostAtli, on 01 November 2011 - 06:43 PM, said:

Also, when the move_uploaded_file function fails, there is often a warning shown to explain why. If there is it might explain why it's failing. Add this to the top of the page to show it: (That is if you don't have error reporting enabled already.)
ini_set('display_errors', true);
error_reporting(E_ALL);


Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: single file upload form

Posted 02 November 2011 - 09:05 PM

That's a bit odd. The allowed paths, according to this error, should be these two:
- /var/www/vhosts/XXX.ca/httpdocs
- /tmp
And everything below those paths should be allowed. Your file path certainly seems to fit the first one.
The "/var/www/vhosts/XXX.ca/httpdocs/upload" directory isn't a symbolic link by any chance?

Did you check the $_FILES['data']['error'] element as well? If your upload_tmp_dir path isn't "/tmp" then there might be some problems with the upload itself. (Not that this would explain the error message, really.)
Was This Post Helpful? 0
  • +
  • -

#5 dreamincodehamza  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: -12
  • View blog
  • Posts: 332
  • Joined: 12-September 08

Re: single file upload form

Posted 05 November 2011 - 09:58 AM

Warning: move_uploaded_file() [function.move-uploaded-file]: open_basedir restriction in effect. File(/var/www/vhosts/XXX.ca/httpdocs/upload/help.txt) is not within the allowed path(s): (/var/www/vhosts/XXX.ca/httpdocs:/tmp) in /home/vhosts/XXX.ca/httpdocs/upload_ac.php on line 33

in a bold saying some sort of restriction .Make sure read/write permission are properly set.
And for debugging echo the source destination path as well as print_r($_FILES);
to see any error in uploading file path.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1