Jump to content

[SOLVED] Uploading a new file / deleting the old.


advancedfuture

Recommended Posts

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";
}
}
?>

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.