MartijnD
Tue 15 August 2006, 02:14 am GMT +0300
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..
olaf
Tue 15 August 2006, 09:10 am GMT +0300
MartijnD
Tue 15 August 2006, 11:39 am GMT +0300
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?
olaf
Tue 15 August 2006, 12:04 pm GMT +0300
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?
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.
MartijnD
Tue 15 August 2006, 12:13 pm GMT +0300
ok, i'm going to try.... :-\
MartijnD
Tue 15 August 2006, 12:27 pm GMT +0300
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 ;D
olaf
Tue 15 August 2006, 12:36 pm GMT +0300
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...
MartijnD
Wed 16 August 2006, 01:53 am GMT +0300
ok, another try..
I took the file upload_example.php.
Took this part:
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:
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
if(isset($_POST['Submit']))
Can't imagine that that's all right....
olaf
Wed 16 August 2006, 07:33 am GMT +0300
you have to name the file fields different, please show me the form too (a live link will help)
MartijnD
Thu 17 August 2006, 12:06 am GMT +0300
Set up a test-page with a link to a txt-version of the complete script.
http://www.benbboeken.nl/test 
olaf
Thu 17 August 2006, 09:19 am GMT +0300
Hello,
try this form:
<form name="form1" enctype="multipart/form-data" method="post" action="/test/upload_example.php">
<input type="hidden" name="MAX_FILE_SIZE" value="256000"><br>
<label for="upload">plaatje nr 1</label><input type="file" name="upload_1" size="30"><br clear="all">
<input type="hidden" name="name" size="20" value="1"> <br clear="all">
<label for="upload">plaatje nr 2</label><input type="file" name="upload_2" size="30"><br clear="all">
<input type="hidden" name="name" size="20" value="2"> <br clear="all">
<label for="upload">plaatje nr 3</label><input type="file" name="upload_3" size="30"><br clear="all">
<input type="hidden" name="name" size="20" value="3"> <br clear="all">
<input style="margin-left:120px;" type="submit" name="Submit" value="Submit">
</form>
and this code should work:
<?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.
MartijnD
Thu 17 August 2006, 01:21 pm GMT +0300
Thanx!! We're getting there ;D
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 >:(
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...
olaf
Thu 17 August 2006, 03:47 pm GMT +0300
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.
MartijnD
Fri 18 August 2006, 02:55 am GMT +0300
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...
olaf
Fri 18 August 2006, 09:48 am GMT +0300
This code should work:
<?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>
<body>
<form name="form1" enctype="multipart/form-data" method="post" action="/test/upload_example.php">
<input type="hidden" name="MAX_FILE_SIZE" value="256000">
<label for="upload">plaatje nr 1</label><input type="file" name="upload_1" size="30"><br clear="all">
<input type="hidden" name="name_1" size="20" value="1"> <br clear="all">
<label for="upload">plaatje nr 2</label><input type="file" name="upload_2" size="30"><br clear="all">
<input type="hidden" name="name_2" size="20" value="2"> <br clear="all">
<label for="upload">plaatje nr 3</label><input type="file" name="upload_3" size="30"><br clear="all">
<input type="hidden" name="name_3" size="20" value="3"> <br clear="all">
<input style="margin-left:120px;" type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
MartijnD
Fri 18 August 2006, 11:07 am GMT +0300
Thnx! But now the files rename with timestamps again....
olaf
Fri 18 August 2006, 12:21 pm GMT +0300
Thnx! But now the files rename with timestamps again....
change this value to "false":
$my_upload->rename_file = true; (didn't tested the code)
MartijnD
Fri 18 August 2006, 12:40 pm GMT +0300
This way the script uploads the files without name-changing.........
(why do i have the feeling the solution is so near...)
olaf
Fri 18 August 2006, 12:51 pm GMT +0300
This way the script uploads the files without name-changing.........
(why do i have the feeling the solution is so near...)
I will check this later...
MartijnD
Fri 18 August 2006, 04:10 pm GMT +0300
great! Don't rush...i'm just glad you're still willing to look over it ;-)
MartijnD
Sat 26 August 2006, 03:32 pm GMT +0300
hmmm, i'll think the solution is more difficult....
olaf
Sat 26 August 2006, 06:01 pm GMT +0300
right the setting for
$my_upload->rename_file = true;
must be true I don't understand that script is using the timestamp
add this code for debugging in your example:
print_r($_POST)