Jump to content

check if file exists, rename, etcc


pouncer

Recommended Posts

I've got this code here which uploads a profile image file..

 

		if (isset($_POST['Submit'])) {
							$_accepted_extensions = array('.jpg', '.bmp', '.gif', '.JPG', '.BMP', '.GIF');
							$tmp = pathinfo($_FILES['imageupload']['name']);

							if (!in_array('.' . $tmp['extension'], $_accepted_extensions)) {
								echo "The image you attempted to upload is of the wrong file type.";
							}
							else {
								$target_path = "../profile_images/";
								$nm = basename($_FILES['imageupload']['name']);
								$target_path = $target_path . $nm; 

								if (move_uploaded_file($_FILES['imageupload']['tmp_name'], $target_path)) {
									echo "The file ".  $nm . " has been uploaded to your profile.";

									$user_id = $_SESSION['UserID'];

									$Image_URL = "thumbnail.php?im=" . "profile_images/" . $nm;

									$profile = mysql_query("UPDATE user_profile SET
												Image_URL = '$Image_URL'
												WHERE user_id = '$user_id'
												") or die (mysql_error());
								} 

								else echo "There was an error uploading the image, please try again.";
							}
						}

 

I want to change it though to work with:

if the file already exists on the ftp, rename the filename (possible adding a random 6 digit number to end of filename?) and upload it. also to store this new filename in the sql table .

 

can anyone help me?

Link to comment
Share on other sites

i'm not a really advanced PHP programmer but from what I know you can add the file_exists() like this

 

<?
if (file_exists(filename)) { commands }
elseif (!file_exists(filename)) { commands }
?>

 

on the rename part I have no clue..

 

hope it helped you a little bit further to success =)

Link to comment
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.