advancedfuture Posted January 8, 2008 Share Posted January 8, 2008 I've been working on a site where a user can login and upload a file. The link to the file's location on the hard drive is logged into a database so that when the user uploads ANOTHER file it will delete the old file from the hard drive (saving space!) The only problem is if the user logs for the first time the "old_url" value is blank, so if i user uploads a file from the form for the first time it generates an ugly warning because there is no existing file to delete. Successful! Warning: unlink(upload\) [function.unlink]: Permission denied in C:\wamp\www\videoUpload.php on line 42 i tried making the if statement make so if the database returns a blank value for 'old_value' that it will skip to else. but havent been too successful as of yet. <?php include 'session.php'; $domainName = 'http://www.pcbytescorp.com/'; // Your file name you are uploading $file_name = $_FILES['ufile']['name']; // random digit to add to our file name $random_digit=rand(0000000000,9999999999); //combine random digit to file name to create new file name $new_file_name=$random_digit.$file_name; //$new_file_name = new upload file name $path= "upload/" .$new_file_name; if($ufile != none) { // Upload file to the upload folder and create, a filename in old_url if(copy($_FILES['ufile']['tmp_name'], $path)) { echo "Successful<BR/>"; include 'dbConnect.php'; $fullPath = $domainName . $path; //full URL for 'video_url' //get the 'old_url' value if it currently exists so we can delete the old file. $query = "SELECT old_url FROM users WHERE username = '$username' AND token = '$token'"; $results = mysql_query($query) or trigger_error("FATAL ERROR: " . mysql_error(), E_USER_ERROR); $num = mysql_num_rows($results); while($row=mysql_fetch_array($results)) { $ulink = "upload\\" . $row[0]; } if ($num != NULL) { //If the old_url value is set, delete the old file to make room for the new file. unlink($ulink); $query = "UPDATE users SET old_url = '$new_file_name' WHERE username = '$username' AND token = '$token'"; mysql_query($query) or trigger_error("FATAL ERROR: " . mysql_error(), E_USER_ERROR); } else { //If there is no old_url set, just upload the file. $query = "UPDATE users SET old_url = '$new_file_name' WHERE username = '$username' AND token = '$token'"; mysql_query($query) or trigger_error("FATAL ERROR: " . mysql_error(), E_USER_ERROR); } //Enter the link to video in the database to be referenced later, put in 'video_url' $query = "UPDATE users SET video_url = '$fullPath' WHERE username = '$username' AND token = '$token'"; mysql_query($query) or trigger_error("FATAL ERROR: " . mysql_error(), E_USER_ERROR); mysql_close(); //Debugging code to test the file uploadd / DB queury. //echo "File Name :".$new_file_name."<BR/>"; //echo "File Size :".$_FILES['ufile']['size']."<BR/>"; //echo "File Type :".$_FILES['ufile']['type']."<BR/>"; //echo $fullPath; } else { echo "Error"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/84949-solved-uploading-a-new-file-deleting-the-old/ Share on other sites More sharing options...
advancedfuture Posted January 8, 2008 Author Share Posted January 8, 2008 solved changed if ($num != NULL) to if ($num > 0 && $row[0] != NULL) problem solved! Quote Link to comment https://forums.phpfreaks.com/topic/84949-solved-uploading-a-new-file-deleting-the-old/#findComment-433179 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.