Forum Moderators: coopster
I have a form that uploads files and data, then a second form on an admin side to edit the information. I can add/delete/and modify text info okay, but I don't know how to edit the file upload. For instance, if a pdf had been uploaded, and it was revised I'd need to replace the existing pdf with the new pdf. Here's the code I use to upload the file...can anyone tell me how to modify the code for editing...or point me to a tutorial that covers editing files (I haven't been successful in that aspect when trying to search via Google).
// handle the upload form
if (isset($_POST['submit'])) {
// connect to the database
require_once ('vsadmin/db_conn_open.php');// add the record to the database
$query = "INSERT INTO uploads (file_name, file_size, file_type, title, description, upload_date) VALUES ('{$_FILES['upload']['name']}', '{$_FILES['upload']['size']}', '{$_FILES['upload']['type']}', '$title', '$description', NOW())";
$result = mysql_query($query);
if ($result) {
// create the file name
$extension = explode ('.', $_FILES['upload']['name']);
// upload ID
$uid = mysql_insert_id();
$filename = $uid . '.' . $extension[1];
// move the file over
if (move_uploaded_file($_FILES['upload']['tmp_name'], "../uploads/$filename")) {
echo '<p>The file has been uploaded!</p>';
} else {
echo '<p><font color="red">The file could not be moved.</font></p>';
// remove the record from the database
$query = "DELETE FROM uploads WHERE upload_id = $uid";
$result = mysql_query ($query);
}
} else { // if the query did not run okay
echo '<p><font color="red">Your upload could not be processed due to a system error.</font></p>';
}
//mysql_close(); // close the database connection
} // End of the main submit conditional
Thanks in advance for any help...
ksd
Thank you. I tried looking at the unlink() function on the php.net site, but still don't understand how I'd use it. I believe to edit the file and the other values associated with the file (title, description, author, etc.), I'd need to use UPDATE. I wouldn't want to unlink the image if only the title or description changed though.
My attempt at using UPDATE with a file results in no file being linked to the document, and the file size and upload date don't register any values:
$query = "UPDATE uploads SET upload_id = '$upload_id', file_name = '$file_name', file_size = '$file_size', file_type = '$file_type', title = '$title', description = '$description', upload_date = '$upload_date' WHERE upload_id = '$upload_id' LIMIT 1";
$result = mysql_query ($query) or die ("Error in query: $query. " . mysql_error());if ($result) {
// create the file name
$extension = explode ('.', $_FILES['upload']['name']);
// upload ID
// $uid = mysql_insert_id();
$uid = $_POST['uid'];
$filename = $uid . '.' . $extension[1];
// move the file over
if (move_uploaded_file($_FILES['upload']['tmp_name'], "../uploads/$filename")) {
echo '<p>The file has been uploaded!</p>';
} else {
echo '<p><font color="red">The file could not be moved.</font></p>';
I'm still at a loss as to how the UPDATE would work when a file is involved. Are there any tutorials or something that would show that, or does someone have a example of code that I could learn from?
Thanks...
Then you can also update the information about this file in your mysql database, with the update query that you posted, provided that you know the ID of the old record in mysql (I assume that $upload_id is the primary key of the old record).
If you change the information in the database without reuploading the file, you just don't unlink the old file and just make the update query. But this information has to be entered manually in a html form.
check if the file was uploaded with
if($_FILES['name_of_file_field']['name']){
//unlink old file
//move new file
}
If no file was uploaded, this condition won't be true so the code won't execute.
What are the values of the variables in your update query string? Where did you get them?
Thanks, I'll try what you've shown...
The upload id variable is carried over from the uploads_admin.php page, which lists all the uploads and has an edit / delete link beneath them, like so:
<a href=\"edit_uploads.php?uid={$row['upload_id']}\">edit</a> ¦ <a href=\"delete_uploads.php?uid={$row['upload_id']}\" onclick=\"javascript:return confirm('Are you sure you want to delete record #{$row['upload_id']} - {$row['title']}?')\">delete</a>
It presents a form identical to the one that uploaded the file and info in the first place... but has the values prefilled based on the upload_id that was passed to it.
Since the file field is a file instead of text, I can't get that value (the file name) to appear in the appropriate field. So if I don't browse and choose something, the script thinks there is no upload ...
Thanks,
ksd
But if you only want to change the file info, whitout uploading a new file, you have to manually enter that info in text fields in a form... but I wonder why you'd want to change a file's info without changing the file itself. If you change, for example, the file's extension, that means that the new file is a different file than the old one... so you uploaded a newer file anyway.
I don't think I really understand what you're trying to do. If you click on 'edit', what do you want to edit?