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
https://forums.phpfreaks.com/topic/45936-check-if-file-exists-rename-etcc/
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 =)

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.