Jump to content

image upload


coolphpdude

Recommended Posts

hey guys,

 

right, i need help picking apart the upload of a file. I'm at the point where i can upload the file to where i want it to go. what i want/need to make sure is that when the user uploads an image it is resized (i will do this later) and if the filename already exists then it is renamed. I currently have the following code:

 


if ((($_FILES["uploadedfile"]["type"] == "image/gif")
|| ($_FILES["uploadedfile"]["type"] == "image/jpeg")
|| ($_FILES["uploadedfile"]["type"] == "image/pjpeg")
|| ($_FILES["uploadedfile"]["type"] == "image/png")
|| ($_FILES["uploadedfile"]["type"] == "image/bmp"))
&& ($_FILES["uploadedfile"]["size"] < 2000000))
{

// Where the file is going to be placed 
$target_path = "uploads/$userid/$classid/";

/* Add the original filename to our target path.  
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
$_FILES['uploadedfile']['tmp_name']; 

echo "$target_path";
echo "<p>";


if (file_exists($target_path))
{
    echo "The file ".  basename( $_FILES['uploadedfile']['name'])." exists";

echo "<p>";

// make the random file name
$randname = md5(rand() * time());
echo "$randname";
$target_path = $target_path . $randname;

echo "<p>";
echo "$target_path";


}
else
{
		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!";
	}
}
}
else
{
echo "invalid file type";
}


 

...This works ok to upload it to the location i want it to go but i problem is with pulling the filename apart to rename it if it already exists. Any help?

 

Cheers

Link to comment
https://forums.phpfreaks.com/topic/74596-image-upload/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.