uploading in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tonyacunar
Forum Newbie
Posts: 8
Joined: Thu May 11, 2006 11:03 am

uploading in php

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Moved to PHP Code :: Snippets is for Posting code for others to use ;)
shawnlarrabee
Forum Newbie
Posts: 6
Joined: Mon Apr 17, 2006 10:14 pm

Post 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.
tonyacunar
Forum Newbie
Posts: 8
Joined: Thu May 11, 2006 11:03 am

sorry

Post 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
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post 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']
User avatar
$phpNut
Forum Commoner
Posts: 40
Joined: Tue May 09, 2006 5:13 pm

Post 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 ...
hamdiya
Forum Newbie
Posts: 3
Joined: Wed May 10, 2006 1:52 pm

Post 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?)
tonyacunar
Forum Newbie
Posts: 8
Joined: Thu May 11, 2006 11:03 am

thanks

Post 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
Post Reply