Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I am trying to make a simple file upload form using PHP. Here's my code:

<?php
    $uploads_dir = '/uploads';

    if(isset($_FILES['thefile'])){
      $errors= array();
      $file_name = $_FILES['thefile']['name'];
      $file_size =$_FILES['thefile']['size'];
      $file_tmp =$_FILES['thefile']['tmp_name'];
      $file_type=$_FILES['thefile']['type'];
      $tmp_name = $_FILES['thefile']["tmp_name"];

      if($file_size > 2097152){
         $errors[]='File size must be less than 2 MB';
      }
      if(empty($errors)==true){
         move_uploaded_file($tmp_name, "$uploads_dir/$file_name");
         echo "Success";
      }
      else{
         print_r($errors);
      }
   }
?>
<!DOCTYPE html>
<html>
<head>
    <title>Simple File Upload</title>
</head>
<body>

<form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="thefile" />
    <input type="submit"/>
</form>

</body>
</html>

I realize that I'm not limiting file types, but I'll worry about that once I can get a simple .jpg or .zip file uploaded. Using the above code, I go to the page on my local server located at

C:\wamp\www\simpleupload  (this contains index.php, the file posted above)

When I select a small image file and click submit, I'm presented with the following errors:

Warning: move_uploaded_file(/uploads/session_timeout_formatting_bug.png): failed to open stream: No such file or directory in C:\wamp\www\project_fileshare\index.php on line 18

and

Warning: move_uploaded_file(): Unable to move 'C:\wamp\tmp\phpEFDE.tmp' to '/uploads/session_timeout_formatting_bug.png' in C:\wamp\www\project_fileshare\index.php on line 18

Line 18 is the line that calls the move_uploaded_file() function.

How do I fix this error? I have an 'uploads_dir' folder located in the same folder as my index.php file. (reference the file path above). What am I doing wrong here? I must be misunderstanding some small part of this process and have put my directory in the wrong place, or I'm doing something wrong in the code.

Can someone spot my mistake and tell me what I need to do to fix it?

share|improve this question
    
try a full system path /var/usr/public/uploads or relative path ../uploads. Plus make sure that if the folder does exist, has proper permissions to write to it. – Fred -ii- 2 days ago
    
FYI the first if statement is not doing nothing there, you dont validate files that way. google for is_upload_ok.. somehing like that... i dont remember, but i know that that line is not doing nothing there – Ariel Maduro 2 days ago
    
And also get the root directory from DIR__ that will be easy to find to root of the file – Ariel Maduro 2 days ago

3 Answers 3

up vote 1 down vote accepted

You are working on windows and you've told PHP a location that is inaccessible (i.e. /uploads linux path).

Since you are working in windows and your document root is C:\wamp\www\simpleupload

Which means, your files are located like this:

  • C:\wamp\www\simpleupload\index.php (your upload script)
  • C:\wamp\www\simpleupload\uploads (where the files should be uploaded)

Why don't you use absolute path like this:

$uploads_dir = getcwd() . DIRECTORY_SEPARATOR . 'uploads';

The getcwd() function will return the current working directory for the executing PHP script (in your case index.php), so the $uploads_dir will now look like this: C:\wamp\www\simpleupload\uploads

Try this.

share|improve this answer
    
I'll give it a shot when I get home! – jros 2 days ago
    
Worked perfectly :) thanks!! – jros yesterday
    
@jros Look at my answer, I'm not saying that will solve your problem, but its an important step for your code =) – Ariel Maduro yesterday

If your upload directory is in the same location as your index.php remove the "/" in your $uploads_dir variable. This, or add a "." before the slash because now it refers to the root which might be something else then your current working directory. Speaking of the devil; http://php.net/manual/en/function.getcwd.php

$uploads_dir = getcwd() . '\uploads';
$uploads_dir = __DIR__ . '\uploads'; #php > 5.6

Also, check if your directory is writeable for php: http://php.net/manual/en/function.is-writable.php

Also what Latheesan said, better to go cross platform as I made the same mistake seen in my edit.

<?php
    function buildPath(...$segments){
        return join(DIRECTORY_SEPARATOR, $segments);
    }

    echo buildPath(__DIR__, 'uploads');
?>
share|improve this answer

And i would change

if(isset($_FILES['thefile'])){

for this

if($_FILE['thefile']['error'] === UPLOAD_ERR_OK){

Because I think that this is the best way to know if the user upload a file or the input is blank.

Or

if (isset($_FILE["thefile"]["name"])){
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.