2

I am php page everything was working fine till today morning. Now the page is not uploading any selected file. All that I keep getting is the following error message:

Warning: move_uploaded_file(upload/BrainStream_2009_06_25_23041.zip) [function.move-uploaded-file]: failed to open stream: No such file or directory in C:\xampp\htdocs\vectorization\admin\jobs_edit.php on line 146

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\Documents and Settings\Admin\Local Settings\Temp\php1B2.tmp' to 'upload/BrainStream_2009_06_25_23041.zip' in C:\xampp\htdocs\vectorization\admin\jobs_edit.php on line 146 File could not be uploaded. Please select a valid file. File Name:BrainStream.zip

I have written following code:

$uplfile = $_FILES['uploadfile']['name'];   
$upltmp = $_FILES["uploadfile"]["tmp_name"];
if(!empty($uplfile))
{       
    $ext = explode(".", $uplfile);
    
    $upload_date = date("Y_m_d"); //use this variable to change file name to avoid conflict with same name files
    $upload_dir = "upload/";

    $file_name=$ext[0]."_".$upload_date."_".rand(0, getrandmax()).".".$ext[1];
    
    (move_uploaded_file($upltmp,$upload_dir.$file_name))
 }

I have XAMPP stack installed on my PC which is running WinXP, has 3 GB RAM and ample of Hard disk space.

No matter which size file I select it always give error.

What must be wrong in this code?

2
  • Just a quick note: If rand() is called without any arguments it behaves automatically the way you use it. Commented Jun 25, 2009 at 12:05
  • I see a security flaw: what if someone uploads a file named "filename.php.jpg" with some malicious php code inside? Instead of $ext[1] use $ext[count($ext)-1]. Commented Jun 25, 2009 at 12:08

6 Answers 6

2

Interesting syntax in the last line. The error indicates the problem is in that line and either the source file or destination directory is missing. Since the first one is automatically generated, make sure that C:\xampp\htdocs\vectorization\admin\upload exists and is writable.

Sign up to request clarification or add additional context in comments.

Comments

1

@phihag,

Thanks for the hint. One of the new developer while studying the source had by mistake remove (../) in the $upload_dir variable assignment.

$upload_dir = "upload/"; //this is wrong

Actually it was set as

$upload_dir = "../upload/"; //this works but accidentally edited by another developer 

What a lamer I am. I could not spot the problem.

Anyways thanks once one for your help to solve my problem.

Comments

0

looks like your problem could be that you're using forward slashes for your upload directory but on windows this would be a backslash, you also need to make sure that the upload directory is relative to the script. if not provide a full path.

A good tip to prevent the slashes issue is to use the DIRECTORY_SEPARATOR constant

2 Comments

Windows' directory separators are \ and /, so that's not a problem. Generally, use DIRECTORY_SEPARATOR in php or just /.
oh really, i never work on windows but had always kinda assumed the reason for DIRECTORY_SEPARATOR was to prevent issues like this. If not, then whats its purpose?
0

One of two things - either

  1. There is no upload directory
  2. There is no file php1B2.tmp

Comments

0

This may seem obvious, but be sure to double check your php.ini

file_uploads = On
upload_tmp_dir = "C:\xampp\tmp"
upload_max_filesize = 64M

Comments

0

What might be useful for you to do is a quick check to ensure the file has been uploaded successfully e.g.

switch ($_FILES["cv"]["error"])
{
    case UPLOAD_ERR_FORM_SIZE:
       // handle error
    case UPLOAD_ERR_INI_SIZE:
       // handle error
    case UPLOAD_ERR_PARTIAL:
       // handle error
    case UPLOAD_ERR_NO_FILE:
       // handle error
    case UPLOAD_ERR_CANT_WRITE:
       // handle error
}

Its a better way of handling the errors you may encounter when uploading files.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.