Click to See Complete Forum and Search --> : File Upload Problem
kevinmcqueen
02-17-2005, 12:10 PM
Hi,
I use the following code to upload a file to a specific folder on server...
However, if a file with the same name, already exists, how can I over get it to not overwrite the existing file?
if ($_FILES['Image'] ['error'] > 1)
{
echo 'Problem: ';
switch ($_FILES['Image'] ['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;
}
if (!$_POST['Image'] == ' ')
{
$upfile = 'Attachments/'.$_FILES['Image'] ['name'] ;
}
if (is_uploaded_file($_FILES['Image'] ['tmp_name']))
{
if (!move_uploaded_file($_FILES['Image'] ['tmp_name'], $upfile))
{
exit;
}
}
phpnovice
02-17-2005, 01:30 PM
Just pre-check for the existence of the file on your server. If it exists, add a sequence number to the file name until it is unique.
kevinmcqueen
02-18-2005, 07:09 AM
how you go about doing that?
Sorry, I'm just new with this PHP stuff...never even touched it till about amonth ago
bokeh
02-18-2005, 04:12 PM
Hi I'm just a novice like you. I wrote the following but haven't tried it but hope it helps you go in the right direction.
$upfile = 'Attachments/'.$_FILES['Image'] ['name'] ;
if (file_exists ($upfile)) {
$num = '1';
while (file_exists ($num . $upfile)) {
$num++;
}
}
$upfile = $num . $upfile;
I hope this helps you. If so post a reply.
bokeh
02-18-2005, 04:18 PM
$upfile = 'Attachments/'.$_FILES['Image'] ['name'] ;
if (file_exists ($upfile)) {
$num = '1';
while (file_exists ($num . $upfile)) {
$num++;
}
$upfile = $num . $upfile;
}
phpnovice
02-18-2005, 04:56 PM
Originally posted by kevinmcqueen
how you go about doing that?
I was going to reply to you with some code when I got home from work, but I see someone else beat me to it. ;-) Mine would have been slightly more involved -- as I would like the sequence number placed on the end of the file name (just before the file extension).
Speak up if you need further help.
bokeh
02-18-2005, 05:05 PM
I'd like to see code if you already have it prepared. I wrote it this way because it is so simple. Only problem with my way is the files don't follow in alphabetical order.
phpnovice
02-18-2005, 05:34 PM
Originally posted by bokeh
I'd like to see code if you already have it prepared.
OK, the following is partially tested -- I just threw it together.
$upFile = $t = 'Attachments/'.$_FILES['Image']['name'];
$upPath = $_SERVER["DOCUMENT_ROOT"].'/uploads/';
$i = 0;
while(file_exists($upPath.$upFile)):
$i++;
$upFile = str_replace('.', '_'.$i.'.', $t);
endwhile;
move_uploaded_file($_FILES['Image']['tmp_name'], $upFile);
kevinmcqueen
02-18-2005, 05:55 PM
OK, got it renaming the file, but its not inserting the updated file name into the database, instead, its inserting the original file name
bokeh
02-18-2005, 05:59 PM
You need to enter it into the database after it has recieved its new name not before.
kevinmcqueen
02-18-2005, 06:34 PM
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'jpg)' at line 1
error message?
phpnovice
02-18-2005, 07:00 PM
Post some code to go with that error message so we can see what your logic looks like.
kevinmcqueen
02-18-2005, 07:12 PM
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
if ($_FILES['Image'] ['error'] > 5)
{
echo 'Problem: ';
switch ($_FILES['Image'] ['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;
}
//does the file have the rigt MIME type?
//if ($_FILES['userfile'] ['type'] != 'text/plain')
//{
//echo 'Problem: file is not plain text';
//exit;
//}
//put the file where we'd like it
if (!$_POST['Image'] == ' ')
{
$upfile = $t = 'Attachments/'.$_FILES['Image']['name'];
$i = 0;
while(file_exists($upfile)):
$i++;
$upFile = str_replace('.', '_'.$i.'.', $t);
endwhile;
move_uploaded_file($_FILES['Image']['tmp_name'], $upfile);
}
if (is_uploaded_file($_FILES['Image'] ['tmp_name']))
{
if (!move_uploaded_file($_FILES['Image'] ['tmp_name'], $upfile))
{
exit;
}
}
$insertSQL = sprintf("INSERT INTO attheracinPosts (poster, threadID, content, madeon, title, avatar, sig, attached) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['user'], "text"),
GetSQLValueString($_POST['ThreadID'], "int"),
GetSQLValueString($_POST['content'], "text"),
GetSQLValueString($_POST['madeon'], "text"),
GetSQLValueString($_POST['title'], "text"),
GetSQLValueString($_POST['profilepic'], "text"),
GetSQLValueString($_POST['sign'], "text"),
GetSQLValueString($_FILES['Image'] ['name'],"text"));
phpnovice
02-18-2005, 07:20 PM
To start with, condense this:
if (!$_POST['Image'] == ' ')
{
$upfile = $t = 'Attachments/'.$_FILES['Image']['name'];
$i = 0;
while(file_exists($upfile)):
$i++;
$upFile = str_replace('.', '_'.$i.'.', $t);
endwhile;
move_uploaded_file($_FILES['Image']['tmp_name'], $upfile);
}
if (is_uploaded_file($_FILES['Image'] ['tmp_name']))
{
if (!move_uploaded_file($_FILES['Image'] ['tmp_name'], $upfile))
{
exit;
}
}
into this:
if (!$_POST['Image'] == ' '):
if (is_uploaded_file($_FILES['Image']['tmp_name'])):
$upfile = $t = 'Attachments/'.$_FILES['Image']['name'];
$i = 0;
while(file_exists($upfile)):
$i++;
$upfile = str_replace('.', '_'.$i.'.', $t);
endwhile;
if (!move_uploaded_file($_FILES['Image']['tmp_name'], $upfile)):
exit;
endif;
endif;
endif;
kevinmcqueen
02-18-2005, 07:26 PM
done that :-s Me lost
phpnovice
02-18-2005, 07:27 PM
Then, change this last line of where you're building your SQL string:
GetSQLValueString($_FILES['Image'] ['name'],"text"));
to this:
GetSQLValueString($upfile,"text"));
kevinmcqueen
02-18-2005, 07:31 PM
sooperb :-)
Thank You All
phpnovice
02-18-2005, 07:32 PM
All working now?
kevinmcqueen
02-19-2005, 04:07 PM
yep, appears to be
Thanks
phpnovice
02-19-2005, 05:20 PM
Cheers.