/    Sign up×
Community /Pin to ProfileBookmark

Uploading images/files

I am somewhat a newbie to PHP. I am trying to create a file upload. It seems to upload to the tmp directory, but fails to move when using move_upload_file(). When checking the $_FILES array after uploading, it shows that everything should be good, including 0 errors. Not sure what I’m doing wrong. HELP!

Here is my code:

<?php
if (isset($_POST[‘submit’])) {
if (isset($_
FILES[‘imageUpload’])) {
$imageName = $_FILES[‘imageUpload’][‘name’];
echo $imageName;
echo “<br />”;
if (move_uploaded_file($_
FILES[‘imageUpload’][‘temp_name’], “uploaded/” . $imageName) == TRUE) {
chmod(“uploaded/” . $_FILES[‘imageUpload’][‘name’], 0644);
echo “File “” . $imageName . “” successfully uploaded. <br />n”;
}
else {
echo “There was an error uploading “” . htmlentities($_
FILES[‘imageUpload’][‘name’]) . “”. <br /> n”;
print_r($_FILES);
}
}
}
?>

<form action=”inc_UploadImage.php” method=”post” enctype =”multipart/form-data”>
Select an image to upload:
<input type=”hidden” name=”MAX_FILE_SIZE” value=”5000000″ />
<input type=”file” name=”imageUpload”><br />
<input type=”submit” value=”Upload Image” name=”submit” />
<input type=”reset” value=”Reset Field” name=”reset” />
</form>

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@hyperionXSMar 04.2012 — Here you will find a very good Upload tutorial: Easy way to upload with PHP and HTML
Copy linkTweet thisAlerts:
@jriversauthorMar 05.2012 — Finally found the mistake!

I was putting: ['temp_name'] instead of ['tmp_name']

Thanks for the help.
Copy linkTweet thisAlerts:
@m610Aug 11.2012 — I am also a noobie and have seen many excellent tutorial like the one linked to above, and it all makes sense except ...

How would one do this if:

  • 1. The form was on one page.

  • 2. That form contained 2 or more “files” inputs.

  • 3. The upload would be done on another page?


  • For example, the user enters data on one form, checks it on a second, and then the 3rd form handles the upload. How would I pass the $_FILES info along and how do I tell the upload routine to upload each file in turn?

    Thanks.
    Copy linkTweet thisAlerts:
    @m610Aug 16.2012 — After spending the weekend plus on this, again, self-teaching from the Internet, I figured out this would not be possible. I had been learning the syntax and all before the working concepts, and the many, many examples I read that told me how to upload files never mentioned how it all actually works.

    When you submit and enctype is set properly the file is uploaded to a temporary folder on the server. The php code on the next page (action) looks at the information provided by $_FILES, decides whether or not to accept it, and if it does it simply moves the temp file to the destination folder.

    Maybe that will help someone like myself.

    I'm still wondering is during a submit I can upload more than one file then handle that in the 'action' page's php script. When I get time I'll try a few things.

    Mike
    ×

    Success!

    Help @jrivers spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.18,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...