Topic: Renaming en uploading multiple files (Read 3692 times)
My name is Bong, James Bong
Gender:
Posts: 12
76 credits Members referred : 0
« on: Aug 15, 2006, 01:14:32 am »
First: Great scripts!!
I am trying to combine two example scripts, but it won't work the way i want to.... I want to upload multiple files, en renaming them the same time (every file seperatly). To do this i thought to combine the example-scripts upload_example.php and multiple_upload_example.php. The result is that i'm still able to upload multiple files, but the script is renaming them with a timestamp-value.
I know i'm a newby to php, but hey, at least i'm trying
Can you help..
Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6691
34714 credits Members referred : 374
My name is Bong, James Bong
Gender:
Posts: 12
76 credits Members referred : 0
« Reply #2 on: Aug 15, 2006, 10:39:02 am »
Hhhhmmmmm......
I already checked that post, but there you work with an MySQL database. My knowledge about databases goes not any further than fill in the user/password and given database-name. That's the reason i took the other two files and tried to melt them to one.
Is it an option that i post the test-file i have now, and you take a look at it?
Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6691
34714 credits Members referred : 374
I already checked that post, but there you work with an MySQL database. My knowledge about databases goes not any further than fill in the user/password and given database-name. That's the reason i took the other two files and tried to melt them to one.
Is it an option that i post the test-file i have now, and you take a look at it?
I don't want to show you how it works together with a database, check the code how the single upload example is used inside a loop to upload multiple files.
My name is Bong, James Bong
Gender:
Posts: 12
76 credits Members referred : 0
« Reply #4 on: Aug 15, 2006, 11:13:24 am »
ok, i'm going to try....
My name is Bong, James Bong
Gender:
Posts: 12
76 credits Members referred : 0
« Reply #5 on: Aug 15, 2006, 11:27:22 am »
OK, this goes way beyond my knowledge about PHP. The problem is not that i can't upload multiple files, that part works fine. The problem lays in the fact i want to rename them at the same time. That works also, but the script give the files his own timestamp-name instead of the name i fill in in the form-field.
Maybe i'm asking too much. In that fact i will will just work with the upload_example.php-script, and do that 5 times
Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6691
34714 credits Members referred : 374
It's time to use PHP5!
« Reply #6 on: Aug 15, 2006, 11:36:14 am »
the only thing you need to reed is this part from the manual: http://www.php.net/foreach to understand a loop.
changing the multiple upload is much more work and actually is "looping" the single upload much more flexible...
My name is Bong, James Bong
Gender:
Posts: 12
76 credits Members referred : 0
« Reply #7 on: Aug 16, 2006, 12:53:23 am »
ok, another try..
I took the file upload_example.php.
Took this part:
Code:
if(isset($_POST['Submit'])) { $my_upload->the_temp_file = $_FILES['upload']['tmp_name']; $my_upload->the_file = $_FILES['upload']['name']; $my_upload->http_error = $_FILES['upload']['error']; $my_upload->replace = (isset($_POST['replace'])) ? $_POST['replace'] : "n"; // because only a checked checkboxes is true $my_upload->do_filename_check = (isset($_POST['check'])) ? $_POST['check'] : "n"; // use this boolean to check for a valid filename $new_name = (isset($_POST['name'])) ? $_POST['name'] : ""; if ($my_upload->upload($new_name)) { // new name is an additional filename information, use this to rename the uploaded file $full_path = $my_upload->upload_dir.$my_upload->file_copy; $info = $my_upload->get_uploaded_file_info($full_path); // ... or do something like insert the filename to the database } }
and changed it into:
Code:
foreach ($_FILES as $key=> $val) { $my_upload->the_temp_file = $_FILES[$key]['tmp_name']; $my_upload->the_file = $val; $my_upload->http_error = $_FILES[$key]['error']; $my_upload->replace = (isset($_POST['replace'])) ? $_POST['replace'] : "y"; // because only a checked checkboxes is true $my_upload->do_filename_check = (isset($_POST['check'])) ? $_POST['check'] : "n"; // use this boolean to check for a valid filename $new_name = (isset($_POST['name'])) ? $_POST['name'] : ""; if ($my_upload->upload($new_name)) { // new name is an additional filename information, use this to rename the uploaded file $full_path = $my_upload->upload_dir.$my_upload->file_copy; $info = $my_upload->get_uploaded_file_info($full_path); // ... or do something like insert the filename to the database } }
Everything shows all-right on screen now, and the script seems to upload the files, but it doesn't. Further i now miss the string
Code:
if(isset($_POST['Submit']))
Can't imagine that that's all right....
Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6691
34714 credits Members referred : 374
It's time to use PHP5!
« Reply #8 on: Aug 16, 2006, 06:33:00 am »
you have to name the file fields different, please show me the form too (a live link will help)
My name is Bong, James Bong
Gender:
Posts: 12
76 credits Members referred : 0
« Reply #9 on: Aug 16, 2006, 11:06:39 pm »
Set up a test-page with a link to a txt-version of the complete script.
<?php include ($_SERVER['DOCUMENT_ROOT']."/test/upload_class.php"); //classes is the map where the class file is stored (one above the root)
$max_size = 1024*250; // the max. size for uploading
$my_upload = new file_upload;
$my_upload->upload_dir = $_SERVER['DOCUMENT_ROOT']."/test/files/"; // "files" is the folder for the uploaded files (you have to create this folder) $my_upload->extensions = array(".jpg", ".gif"); // specify the allowed extensions here // $my_upload->extensions = "nl"; // use this to switch the messages into an other language (translate first!!!) $my_upload->max_length_filename = 50; // change this value to fit your field length in your database (standard 100) $my_upload->rename_file = true;
if (isset($_POST['Submit'])) { foreach ($_FILES as $key=> $val) { $my_upload->the_temp_file = $_FILES[$key]['tmp_name']; $my_upload->the_file = $_FILES[$key]['name']; $my_upload->http_error = $_FILES[$key]['error']; $my_upload->replace = (isset($_POST['replace'])) ? $_POST['replace'] : "y"; // because only a checked checkboxes is true $my_upload->do_filename_check = (isset($_POST['check'])) ? $_POST['check'] : "n"; // use this boolean to check for a valid filename $new_name = (isset($_POST['name'])) ? $_POST['name'] : ""; if ($my_upload->upload($new_name)) { // new name is an additional filename information, use this to rename the uploaded file $full_path = $my_upload->upload_dir.$my_upload->file_copy; $info = $my_upload->get_uploaded_file_info($full_path); // ... or do something like insert the filename to the database } } }
This example is not tested but should help you to start the right way.
My name is Bong, James Bong
Gender:
Posts: 12
76 credits Members referred : 0
« Reply #11 on: Aug 17, 2006, 12:21:09 pm »
Thanx!! We're getting there
At least i'm able to upload again, and the script is renaming the files the same time. The only problem is, the script is renaming all the files the same name now....hmmm
Code:
Foto succesvol kopieert. Het gekopieerde bestand is hernoemd naar 3.jpg. Foto succesvol kopieert. Het gekopieerde bestand is hernoemd naar 3.jpg. Foto succesvol kopieert. Het gekopieerde bestand is hernoemd naar 3.jpg.
i've tried renaming some variables, but i think it's a lot more complicate than that...
Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6691
34714 credits Members referred : 374
It's time to use PHP5!
« Reply #12 on: Aug 17, 2006, 02:47:42 pm »
just give them the same name like the file fields or use numbers inside the names and use the function "substr" to use the trailing number as a variable.
My name is Bong, James Bong
Gender:
Posts: 12
76 credits Members referred : 0
« Reply #13 on: Aug 18, 2006, 01:55:20 am »
I'm getting a little ashamed , but i really don't understand what you're talking about. I read several topics about 'substr' but i guess i'm to much of an newby for this kind of scripting. Too bad, 'cause i think the script is very close to what i need.... Thanks anyway for the trouble...
Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6691
34714 credits Members referred : 374
It's time to use PHP5!
« Reply #14 on: Aug 18, 2006, 08:48:01 am »
This code should work:
Code:
<?php include ($_SERVER['DOCUMENT_ROOT']."/test/upload_class.php"); //classes is the map where the class file is stored (one above the root)
$max_size = 1024*250; // the max. size for uploading
$my_upload = new file_upload;
$my_upload->upload_dir = $_SERVER['DOCUMENT_ROOT']."/test/files/"; // "files" is the folder for the uploaded files (you have to create this folder) $my_upload->extensions = array(".jpg", ".gif"); // specify the allowed extensions here // $my_upload->extensions = "nl"; // use this to switch the messages into an other language (translate first!!!) $my_upload->max_length_filename = 50; // change this value to fit your field length in your database (standard 100) $my_upload->rename_file = true;
if (isset($_POST['Submit'])) { foreach ($_FILES as $key=> $val) { if (!empty($_FILES[$key]['name'])) { $my_upload->the_temp_file = $_FILES[$key]['tmp_name']; $my_upload->the_file = $_FILES[$key]['name']; $my_upload->http_error = $_FILES[$key]['error']; $my_upload->replace = "y"; $my_upload->do_filename_check = "n"; $new_name_key = "name_".strrchr($key, "_"); $new_name = $_POST[$new_name_key]; if ($my_upload->upload($new_name)) { // new name is an additional filename information, use this to rename the uploaded file $full_path = $my_upload->upload_dir.$my_upload->file_copy; $info = $my_upload->get_uploaded_file_info($full_path); // ... or do something like insert the filename to the database } } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Multi file upload form</title> </head>