Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.