Page 1 of 1

uploading in php

Posted: Thu May 11, 2006 11:57 am
by tonyacunar
Hello, I want users to be able to upload images to my server. I tried
using the code from the PHP manual website but my servers PHP log
catches a syntax error where there is none. I have a folder called
images, and my apache/php is set to accept files. Here is the code
perhaps you guys have any tips or another tutorial.
thanks

this is my first file, i dont think it has anything wrong with it
index.html

Code: Select all

<form enctype="multipart/form-data" action="uploader.php" method="POST">
   <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
   Send this file: <input name="userfile" type="file" />
   <input type="submit" value="Send File" />
</form>
Here is the second file uploader.php

Code: Select all

$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
Im running a Apache php mySql package for OSX called MAMP
thanks
-tony

Posted: Thu May 11, 2006 12:07 pm
by Chris Corbyn
Moved to PHP Code :: Snippets is for Posting code for others to use ;)

Posted: Thu May 11, 2006 1:12 pm
by shawnlarrabee
You said that you have a folder called images, but your upload directory/target path is set to uploads/. That might be the problem.

sorry

Posted: Thu May 11, 2006 1:50 pm
by tonyacunar
sorry, I meant to say that folder, my folder is good. My php error log actually gives me a syntax error which is really weird. Thanks

Posted: Fri May 12, 2006 2:58 am
by jmut
why would you double post. I don't get it.
viewtopic.php?t=48294


AND there is not such index.....

Code: Select all

$_FILES['uploadedfile']['name']

Posted: Fri May 12, 2006 5:44 am
by $phpNut

Code: Select all

$_FILES['uploadedfile']
? I thought in your first post it the file object had a name of userfile?

surely you should be using

Code: Select all

$_FILES['userfile']
and is that the line the error is on?

So you need to change that for starters ...

What is the synyax error?

Edit: it's also better to rename the file rather than give it the same name, somthing unique for all. My imagehost uploads them in to this format:

Code: Select all

$alphanum = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; // Giant string
$rand = substr(str_shuffle($alphanum), 0, 3);  // Gets a random 3 from the Giant string

$path_parts = pathinfo($_FILES['uploadedfile']['name']); // Gets file attributes

$fileExt = strtolower($path_parts['extension']);  // obtains extension from pathinfo above
$fileURL =  date("d-m-Y_H:i") . $rand . "." . $fileExt; // Name would be on the lines 28-04-2006_01:21wcK.jpg
That way there is a very small chance that the fiule names will be the same. even add seconds if you wish ...

Posted: Fri May 12, 2006 6:18 am
by hamdiya
Also make sure the directory where the images will be moved to can be written to by PHP. CHMOD the directory to 777 (or is it 666?)

thanks

Posted: Fri May 12, 2006 8:14 am
by tonyacunar
thanks guys, I got the upload working. Those are all good tips, I think im going to implement that image renaming thing too
-tony