Upload fail

Can anyone see why my upload would be failing? It’s hitting my “else” statement.

$target_dir = "../uploads/";
        $target_file = $target_dir . basename($_FILES["articleFile"]["name"]);
        if(move_uploaded_file($_FILES["articleFile"]["name"], $target_file))
        {
          echo "<div class=\"disclaimer\"><span>Congratulations</span>The file ".  basename( $_FILES['articleFile']['name']). " has been uploaded</div>";
        }
        else
        {
          echo "<div class=\"error\"><span>Error</span>There was an problem uploading the file, please try again!</div>";
        }

Echoing $target_file outputs “…/uploads/” so I think it’s something wrong with the move function? Can anyone spot what I’m doing wrong? I had it working but my brain is failing me. I must be missing something obvious. I had it working not too long ago…

Shouldn’t that be “uploads/”? Unless you are using this script other than the root directory

Surly this should have a filename on the end not just the folder name?

Have you tried print_r($_files) and see what you have - I think print_r($_files) is the correct code to try.

I should have explained.

On my root directory I have a bunch of folders - the two important ones here are “uploads” and “admin”

In the admin folder, I’m doing the upload.

So I believe the uploads echo is correct since I’m going back to the root and into “uploads/”

Thanks for looking at it.

Oops - I only copoied that line to a new file to do the echo. The HTML form has name=“articleForm” on the upload though.

Wild guess. Folder permissions?

It’s worked in the past before. I was just switching up some sanitation stuff.

This is now my submit page.

<?php
       $target_dir = "../uploads/";
        $target_file = $target_dir . basename($_FILES["articleFile"]["name"]);
echo $target_file;
echo $_FILES["articleFile"]["name"];
        if(move_uploaded_file($_FILES["articleFile"]["name"], $target_file))
        {
          echo "<div class=\"disclaimer\"><span>Congratulations</span>The file ".  basename( $_FILES['articleFile']['name']). " has been uploaded</div>";
        }
        else
        {
          echo "<div class=\"error\"><span>Error</span>There was an problem uploading the file, please try again!</div>";
        }
?>

It echos out

…/uploads/download.jpgdownload.jpg
ErrorThere was an problem uploading the file, please try again!

Should be

if(move_uploaded_file($_FILES["articleFile"]["tmp_name"], $target_file))
1 Like

Heh, saw your “temp” suggestion first…but yeah that works now. Thank you.

My “name” worked before though, and I know I didn’t have “tmp_name”. What’s that supposed to reference anyway? What is tmp_name?

If you add print_r() to your page you will see the keys. tmp_name is where it is stored before it is moved.

echo "<pre>";    
print_r($_FILES);    
echo "</pre>";
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.