Jump to content


Photo

Another Image Upload Problem


  • Please log in to reply
7 replies to this topic

#1 GrizNeT

GrizNeT
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 23 October 2006 - 05:49 PM

Hi to all,
I have scanned most of the forum and found that most of the questions related to my problem is focused on uploading a file and not to overwrite it but to create a new one.
My problem is a little bit different.

First, 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)

Second, when the same user uploaded another file it will first check if a file is existing, if a file exists then it will unlink/delete the file and upload the new file and if no file exists then it will upload the file.

My problem here is the second one.. i have trouble finding if a file is existing. i know im a bit closer but i need your help guys. Many thanks

Here is my code:

<html>
<head>

<?php
require_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>


#2 GrizNeT

GrizNeT
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 23 October 2006 - 09:02 PM

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

#3 GrizNeT

GrizNeT
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 24 October 2006 - 01:34 PM

*Bump*

#4 gmwebs

gmwebs
  • Members
  • PipPipPip
  • Advanced Member
  • 174 posts
  • LocationLondon

Posted 24 October 2006 - 03:11 PM

Does your script currently work? The logic seems ok at a quick glance...

#5 True`Logic

True`Logic
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 24 October 2006 - 03:17 PM

$myname = 0;

while (file_exist($myname . "." . $ext)) {
$myname++;
}

$myfile = $myname . "." . $ext


hope this is self explanitory enough =)

#6 GrizNeT

GrizNeT
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 24 October 2006 - 03:31 PM

Hi gmwebs :D

The 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..

#7 GrizNeT

GrizNeT
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 24 October 2006 - 03:33 PM

Hi True`Logic,

Thanks for the reply. I'll give it a try

Kudos to all!

#8 gmwebs

gmwebs
  • Members
  • PipPipPip
  • Advanced Member
  • 174 posts
  • LocationLondon

Posted 24 October 2006 - 03:45 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users