GrizNeT Posted October 23, 2006 Share Posted October 23, 2006 Hi to all, I have scanned most of the forum and found that most of the questions related to my problem is focused on [i]uploading a file and not to overwrite it but to create a new one[/i].My problem is a little bit different. [b]First[/b], any uploaded file is renamed according to the users name and concatenated by the extension that they are using. Please take note that they can only upload 1 file and it will recieve .jpg,gif,png extensions only..ex. (demouser.jpg or demouser.gif or demouser.png)[b]Second[/b], when the same user uploaded another file it will first check if a file is existing, if a file exists then it will [b]unlink/delete [/b] the file and upload the new file and if no file exists then it will upload the file.My problem here is the [b]second one[/b].. i have trouble finding if a file is existing. i know im a bit closer but i need your help guys. Many thanksHere is my code:[code]<html><head><?phprequire_once('includes/config.inc');require_once('includes/constants.php');require_once('includes/functions.php');require_once('includes/mysql_connect.php'); session_start(); //Start session$myid = $_SESSION['myid'];$query = "SELECT cli_name FROM tblclients WHERE cli_id = $myid";$result = @mysql_query($query);$row = mysql_fetch_array($result);$myname = $row[0];?><title>File Uploader </title></head><body><h1>Uploading file...</h1><?php if ($_FILES['userfile']['error'] > 0) { echo 'Problem: '; switch ($_FILES['userfile']['error']) { case 1: echo 'File exceeded upload_max_filesize'; break; case 2: echo 'File exceeded max_file_size'; break; case 3: echo 'File only partially uploaded'; break; case 4: echo 'No file uploaded'; break; } exit; } //Check for correct MIME type if ($_FILES['userfile']['type'] =='image/pjpeg') { $ext = ".jpg"; } elseif ($_FILES['userfile']['type'] =='image/gif') { $ext = ".gif"; } elseif ($_FILES['userfile']['type'] =='image/png') { $ext = ".png"; } else { echo 'Problem: file is not an image'; exit; } //Check if file exists $myfile = '/var/www/html/ecrema/uploads/logos/'.$myname.$ext; $mydir = '/var/www/html/ecrema/uploads/logos/'; if (file_exists($myfile)) { unlink($myfile); //Change the filename to Company's Name + logo $_FILES['userfile']['name'] = $myname.$ext; //Put the file where ur directory is located $upfile = $mydir.$_FILES['userfile']['name']; //echo $upfile.'<br />'; } else { //Change the filename to Company's Name + logo $_FILES['userfile']['name'] = $myname.$ext; //Put the file where ur directory is located $upfile = $mydir.$_FILES['userfile']['name']; //echo $upfile.'<br />'; } if (is_uploaded_file($_FILES['userfile']['tmp_name'])) { if(!move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile)) { echo 'Problem: could not move file to destination directory'; exit; } } else { echo 'Problem: Possible file upload attack. Filename: '; echo $_FILES['userfile']['name']; exit; } echo 'File uploaded sucessfully<br /><br />'; ?></body></html>[/code] Quote Link to comment Share on other sites More sharing options...
GrizNeT Posted October 23, 2006 Author Share Posted October 23, 2006 i hope i explained it clearly.. all i wanted is to know how to check if a file exists and will delete it irregardless of file extension and upload a new file..If no file is found it will upload a new file..Thanks Quote Link to comment Share on other sites More sharing options...
GrizNeT Posted October 24, 2006 Author Share Posted October 24, 2006 *Bump* Quote Link to comment Share on other sites More sharing options...
gmwebs Posted October 24, 2006 Share Posted October 24, 2006 Does your script currently work? The logic seems ok at a quick glance... Quote Link to comment Share on other sites More sharing options...
True`Logic Posted October 24, 2006 Share Posted October 24, 2006 $myname = 0;while (file_exist($myname . "." . $ext)) {$myname++;}$myfile = $myname . "." . $exthope this is self explanitory enough =) Quote Link to comment Share on other sites More sharing options...
GrizNeT Posted October 24, 2006 Author Share Posted October 24, 2006 Hi gmwebs :DThe script works only if a file does exists and it matches the result of the file_exists() function. Then it will delete the existing file and upload the new one... My problem here is: What if there is an existing file pic1.jpg and i want to upload pic1.gif... Using the file_exists() function it will not surely find the pic1.jpg since im uploading pic1.gif.. The result here is that i have 2 files in my directory. What i want is i will only have one file with pic1.jpg(existing) be deleted and be replaced by pic1.gif.. Quote Link to comment Share on other sites More sharing options...
GrizNeT Posted October 24, 2006 Author Share Posted October 24, 2006 Hi True`Logic,Thanks for the reply. I'll give it a tryKudos to all! Quote Link to comment Share on other sites More sharing options...
gmwebs Posted October 24, 2006 Share Posted October 24, 2006 One way would be to use a wildcard (filename.*) and just unlink files with any extension as long as the filename matches.Or maybe you would have to define the acceptable extensions and then iterate through each one. Does filename.jpg exist, does filename.gif exist, does filename.png exist - then unlink each one if it does. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.