Jump to content

Another Image Upload Problem


GrizNeT

Recommended Posts

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 thanks

Here is my code:

[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>
[/code]
Link to comment
Share on other sites

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..
Link to comment
Share on other sites

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