Help : Uploading and renaming files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wlhan
Forum Newbie
Posts: 2
Joined: Thu Dec 09, 2004 2:32 pm
Location: UAE

Help : Uploading and renaming files

Post by wlhan »

Dear all

I'm working on news program in php and i'm facing problem with uploading and renaming the uploaded file then storing the new name in a field in mysql database so could any one help me with it because i really need it fastly


Thanks & Regards
Wlhan
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

do you have an example of the code that you wrote thats not working so we can help you from there?
wlhan
Forum Newbie
Posts: 2
Joined: Thu Dec 09, 2004 2:32 pm
Location: UAE

Post by wlhan »

This is the Uploding code with out the databse part but the renaming option dose not worke
[/php_man]
<?php
// Set error reporting to all (E_ALL) except(^) notices (E_NOTICE)
error_reporting (E_ALL ^ E_NOTICE);

// Lets define our class
class File_Manager {
// Uploads a file.
function Upload_File($Form_Input, $Upload_Path) {
// File specific variables..

// Name of file on local computer with path
$tmp_name = $_FILES[$Form_Input]['tmp_name'];

// Type of file, image/gif or so on
$type = $_FILES[$Form_Input]['type'];

// If an error occurrs, this will change from 0
$error = $_FILES[$Form_Input]['error'];

// Size of file
$size = $_FILES[$Form_Input]['size'];

// Name of file on local computer, without path
$name = $_FILES[$Form_Input]['name'];

// This is to set a new name, the name format would be
// Uploaded_(#Time in seconds from 01/01/1970#)_Size(Files size)_name_it_had_to_begin_with_with_extension
$new_name = "Uploaded(" . time() . ")_Size($size)_$name";

// Attempt to upload the file
$Upload = copy($tmp_name, $Upload_Path . $name);

// Set the results array to an array
$Results_Array = array();
$Results_Array[] = $new_name;
$Results_Array[] = $error;
$Results_Array[] = $type;

return $Results_Array;
}
}

// Process the form if it was submitted
if ($_POST['Submit'] != "") {
// Lets start the class
$File_Manager = New File_Manager;

// The function Upload_File in the class returns an array
// So we list the values of the array so its easier to read
// Upload_File ("name from form field", "upload path");
list($Name, $Error, $Type,) = $File_Manager -> Upload_File("File_To_Upload", "upload/");

// Error management
// If there is no error
if ($Error == "0") {
// Show a message
//echo $new_name;
echo "File uploaded successfully!<br />";
}
else
{
// Else, there is an error, show a message
echo "There was an error uploading your file.<br />";
}
}

// Show the form
// basename($_SERVER['PHP_SELF']) returns the current
// filename, IE if you called http://www.example.com/test/upload.php
// it would return "upload.php"
echo "<form action=\"" . basename($_SERVER['PHP_SELF']) . "\" method=\"POST\" enctype=\"multipart/form-data\">
<input name=\"File_To_Upload\" type=\"file\">
<input type=\"Submit\" name=\"Submit\" value=\"Upload\">
</form>";
?>
Post Reply