Uploading an Image Help

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
sampage
Forum Newbie
Posts: 22
Joined: Sat Mar 18, 2006 6:17 pm

Uploading an Image Help

Post by sampage »

I'm trying to allow users to upload an image for use as there avatar.

At the moment I have this script, very basic and no limitations on file size or type at the moment!!!

Code: Select all

<?php
if($submit)
{

$_FILES["file"]["name"]="FILEONE";

  echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
  echo "Uploading " . $_FILES["file"]["name"];
  echo " (" . $_FILES["file"]["type"] . ", ";
  echo ceil($_FILES["file"]["size"] / 1024) . " Kb).<br />";
  echo "File is temporarily stored as ". $_FILES["file"]["tmp_name"];

    move_uploaded_file($_FILES["file"]["tmp_name"], 
    "uploads/" . $_FILES["file"]["name"]);
    echo "File has been stored in your uploads directory.";

}
?> 
<html>
<body>

<form action="<? echo $PHP_SELF; ?>" method="post" enctype="multipart/form-data">
  <p>File Upload</p>
  <input type="file" name="file" id="file" /> 
  <br />
  <input type="submit" name="submit" value="Submit" />
</form> 

</body>
</html>
I need to be able to rename the image so that it matches a users Primary Key before it is saved to the correct folder. I have there primary key in a variable: $user_number.

How do I go about doing this?
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

change

Code: Select all

#
    move_uploaded_file($_FILES["file"]["tmp_name"],
#
    "uploads/" . $_FILES["file"]["name"]);
to

Code: Select all

#
    move_uploaded_file($_FILES["file"]["tmp_name"],
#
    "uploads/" . $user_number);
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Replace line 13 in your code with this code:

Code: Select all

$file_info = pathinfo($_FILES['file']['name']);

    move_uploaded_file($_FILES['file']['tmp_name'], "uploads/{$user_number}." . $file_info['extension']);
Of course this is not the best way and more checks need to be done to ensure that we get the real and correct file type but this is only a general idea and in a real application I will never do this like this without more serious checks.

Edit: What about the file extension phpScott?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

I wrote an image uploading tutorial. I guess it's in review by the tutorial team, or something.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

Edit: What about the file extension phpScott?
dope!!

Was just so happy I got to answer a post first I overlooked the simple stuff.

Monday, CSS, X-cart doing my head in.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

LOL... that's ok.
Post Reply