poleposters Posted October 23, 2008 Share Posted October 23, 2008 Hi, I have an upload script. It uploads, renames and records the location of a file. It works fine. But It clashes with another script that removes the location of the file from the database. The application is for a print company. So artwork is usually submitted in two or more parts. The upload script works like this. The first file for a particular job is uploaded with the base filename. The second one is uploadeded, A query is performed to check if artwork for the job already exists. And the file name is appended with a number (mysql_num_rows + 1). So the idea each upload is named with the appendage ie print.pdf, print_file2.pdf, print_file3.pdf. The problem is that there is an option to remove the file after it has been uploaded in case of mistakes. So when a file is uploaded to replace the removed file, its name conflict with another name in the database. ie if I remove "print_file2.pdf". The upload script returns two results and names the new file print_file3.pdf which already exists. Basically, I want to modify the script so that in the case above. The new uploaded file will take the place of the one removed. Its similar to creating a new folder on the desktop. If you delete a folder. A new folder will take its place with the same name. The code is below. Any ideas will be appreciated.I'm stuck! if(isset($_POST['Submit'])) { $order=$_GET['order']; $itemid=$_GET['itemnumber']; $item=$_GET['item']; if ($_FILES['picThumbnail']['name'] != "") { $query2="SELECT * FROM artwork WHERE item_id=$itemid"; $result2=mysql_query($query2) or trigger_error("Query: $query2\n<br />MySQL Error: " . mysql_error()); $num=mysql_num_rows($result2); if($num>0) { $number=mysql_num_rows($result2); $number2=$number+1; $name1=$item."_".$date."_".O.$order.I.$itemid."_".FILE.$number2; } else { $name1=$item."_".$date."_".O.$order.I.$itemid; } $date=date("d-m-Y"); $sUploadDir = 'uploadartwork/'; $sUploadedFile = $sUploadDir . basename($_FILES['picThumbnail']['name']); if (move_uploaded_file($_FILES['picThumbnail']['tmp_name'], $sUploadedFile)) { $sThumbnail = $_FILES['picThumbnail']['name']; $ext = strrchr($sThumbnail,'.'); $sThumbnail = $name1.$ext; $sFileName = strtoupper(($sThumbnail)); if (file_exists(($sUploadDir.$_FILES['picThumbnail']['name']))) rename(($sUploadDir.$_FILES['picThumbnail']['name']), ($sUploadDir.$sFileName)); $art=$sUploadDir.$sFileName; $query="INSERT INTO artwork (artwork_id,artwork,item_id) VALUES ('','$art','$itemid')"; $result=mysql_query($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error()); if($result){header("Location: print.php?order=$order");} } } Link to comment https://forums.phpfreaks.com/topic/129722-solved-unique-filename-upload-challenge/ Share on other sites More sharing options...
d_barszczak Posted October 23, 2008 Share Posted October 23, 2008 Why are you using mysql_num_rows + 1 to give your files their unique identifier. You should use a Primary Key with auto increment. The problem you have is that you can duplicate your ids. eg. I have 10 images in my database with ids 1 - 10 so my next id is 11. I delete number 5 so my next id is 10... But 10 already exists. You should use LAST_INSERT_ID() to get your files new id. Insert information into database. Get the LAST_INSERT_ID(). Rename the file. Link to comment https://forums.phpfreaks.com/topic/129722-solved-unique-filename-upload-challenge/#findComment-672593 Share on other sites More sharing options...
poleposters Posted October 23, 2008 Author Share Posted October 23, 2008 Thanks. But I think you misunderstand. The unique identifier is for the name of the file. The script doesn't change the record keys. I found a solution finally! It wasn't as much of a challenge as I expected. Here the code for those interested. <?php $itemid=$_GET['itemnumber']; $item=$_GET['item']; $order=$_GET['order']; if(isset($_POST['Submit'])) { $order=$_GET['order']; $itemid=$_GET['itemnumber']; $item=$_GET['item']; if ($_FILES['picThumbnail']['name'] != "") { $date=date("d-m-Y"); $name1=$item."_".$date."_".O.$order.I.$itemid; $sUploadDir = 'uploadartwork/'; $sUploadedFile = $sUploadDir . basename($_FILES['picThumbnail']['name']); if (move_uploaded_file($_FILES['picThumbnail']['tmp_name'], $sUploadedFile)) { $sThumbnail = $_FILES['picThumbnail']['name']; $ext = strrchr($sThumbnail,'.'); $query2="SELECT * FROM artwork WHERE item_id='$itemid'"; $result2=mysql_query($query2) or trigger_error("Query: $query3\n<br />MySQL Error: " . mysql_error()); $num2=mysql_num_rows($result2); $n=$num2+1; for($i=1; $i<=$n;$i++ ) { $sThumbnail = $name1.$i.$ext; $sThumbnail2=$sUploadDir.$sThumbnail; $query3="SELECT * FROM artwork WHERE artwork='$sThumbnail2'"; $result3=mysql_query($query3) or trigger_error("Query: $query3\n<br />MySQL Error: " . mysql_error()); $num=mysql_num_rows($result3); if($num==0) { $number = $sThumbnail; break; } } $sFileName = strtoupper(($sThumbnail)); if (file_exists(($sUploadDir.$_FILES['picThumbnail']['name']))) rename(($sUploadDir.$_FILES['picThumbnail']['name']), ($sUploadDir.$sFileName)); $art=$sUploadDir.$sFileName; $query="INSERT INTO artwork (artwork_id,artwork,item_id) VALUES ('','$art','$itemid')"; $result=mysql_query($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error()); if($result){header("Location: print.php?order=$order");} } } }?> Link to comment https://forums.phpfreaks.com/topic/129722-solved-unique-filename-upload-challenge/#findComment-672609 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.