Just another rainy day
Posts: 1
6 credits Members referred : 0
« on: Jun 23, 2006, 04:27:48 PM »
upload_db_example.php example work with single file upload.
i want use multi upload form and store filenames in DB, how can i do it?
variables (upload.class.php) $file_copy stores, only the last uploaded filename
Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6280
38506 credits Members referred : 374
It's time to use PHP5!
« Reply #1 on: Jun 23, 2006, 04:52:50 PM »
Hello, the multi upload is just an example how to upload multiple files in one qucik move.
If you have more then one files it works always with an loop for example:
Code:
<?php 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 = "y"; $my_upload->do_filename_check = "n"; // use this boolean to check for a valid filename if ($my_upload->upload()) { // new name is an additional filename information, use this to rename the uploaded file mysql_query(sprintf("INSERT INTO file_table SET file_name = '%s'", $my_upload->file_copy)); } }
I'm not sure that this bug free but it have to work like this, but there is no error handling in this loop
Kill the googlebot
Posts: 6
36 credits Members referred : 0
« Reply #2 on: Nov 27, 2006, 09:57:45 PM »
Hello, im currently using this script with the multi upload, but my sql db is a bit different, i want the users to be able to upload 4 photos. i have 4 rows added into the table called "img01", "img02", "img03", "img04", how would i go about for using this script to add each filename into those 4 fields?
Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6280
38506 credits Members referred : 374
It's time to use PHP5!
« Reply #3 on: Nov 27, 2006, 10:43:14 PM »
please tell me more about what you want to do are these images for on database record?
Kill the googlebot
Posts: 6
36 credits Members referred : 0
« Reply #4 on: Nov 28, 2006, 04:10:56 PM »
i have a java image replace script, i want it so they can upload 4 images, it stores all there names, then loads the file names in php and uses the javascript that when they click the thumbnail it loads the main photo. so i need to store the filename.extention into each field for the images.
Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6280
38506 credits Members referred : 374
i have a java image replace script, i want it so they can upload 4 images, it stores all there names, then loads the file names in php and uses the javascript that when they click the thumbnail it loads the main photo. so i need to store the filename.extention into each field for the images.
do you have 4 db records and 4 images or 1 db record and 4 images?
mysql_query("UPDATE members SET img01 = \"$img01\", img02 = \"$img02\", img03 = \"$img03\", img04 = \"$img04\" WHERE username = \"$username\");
but the $img01 - 04 dont show the results properly, it shows it as an Array when it writes it, but if i do
echo " . $_FILES[upload][name][0] . ";
it will show the first filename exactly how i want to do it. what i need to do/know is how can i use or get the values so i can have a working sql query that will update those 4 rows in my table.
Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6280
38506 credits Members referred : 374
It's time to use PHP5!
« Reply #7 on: Nov 29, 2006, 05:11:29 PM »
right the file array is wrong, why not naming the file fields unique, like:
include ("upload_class.php"); //classes is the map where the class file is stored (one above the root) //error_reporting(E_ALL); $max_size = 1024*500; // the max. size for uploading
class muli_files extends file_upload {
var $number_of_files = 0; var $names_array; var $tmp_names_array; var $error_array; var $wrong_extensions = 0; var $bad_filenames = 0;
function extra_text($msg_num) { switch ($this->language) { case "de": // add you translations here break; default: $extra_msg[1] = "Error for: <b>".$this->the_file."</b>"; $extra_msg[2] = "You have tried to upload ".$this->wrong_extensions." files with a bad extension, the following extensions are allowed: <b>".$this->ext_string."</b>"; $extra_msg[3] = "Select at least on file."; $extra_msg[4] = "Select the file(s) for upload."; $extra_msg[5] = "You have tried to upload <b>".$this->bad_filenames." files</b> with invalid characters inside the filename."; } return $extra_msg[$msg_num]; } // this method checkes the number of files for upload // this example works with one or more files function count_files() { foreach ($this->names_array as $test) { if ($test != "") { $this->number_of_files++; } } if ($this->number_of_files > 0) { return true; } else { return false; } } function upload_multi_files () { $this->message = ""; if ($this->count_files()) { foreach ($this->names_array as $key => $value) { if ($value != "") { $this->the_file = $value; $new_name = $this->set_file_name(); if ($this->check_file_name($new_name)) { if ($this->validateExtension()) { $this->file_copy = $new_name; $this->the_temp_file = $this->tmp_names_array[$key]; if (is_uploaded_file($this->the_temp_file)) { if ($this->move_upload($this->the_temp_file, $this->file_copy)) { $this->message[] = $this->error_text($this->error_array[$key]); if ($this->rename_file) $this->message[] = $this->error_text(16); sleep(1); // wait a seconds to get an new timestamp (if rename is set) } } else { $this->message[] = $this->extra_text(1); $this->message[] = $this->error_text($this->error_array[$key]); } } else { $this->wrong_extensions++; } } else { $this->bad_filenames++; } } } if ($this->bad_filenames > 0) $this->message[] = $this->extra_text(5); if ($this->wrong_extensions > 0) { $this->show_extensions(); $this->message[] = $this->extra_text(2); } } else { $this->message[] = $this->extra_text(3); } } }
$multi_upload = new muli_files;
$multi_upload->upload_dir = $_SERVER['DOCUMENT_ROOT']."/test/photos/$username/"; // "files" is the folder for the uploaded files (you have to create this folder) $multi_upload->extensions = array(".png", ".jpg", ".jpeg", ".gif", ".bmp"); // specify the allowed extensions here $multi_upload->message[] = $multi_upload->extra_text(4); // a different standard message for multiple files //$multi_upload->rename_file = true; // set to "true" if you want to rename all files with a timestamp value $multi_upload->do_filename_check = "y"; // check filename ...
$conn = mysql_connect("localhost", "**editedout**", "**editedout**") or die(mysql_error()); mysql_select_db("website", $conn) or die(mysql_error());
no it didnt, you dont know what or how i should go about doing this?
Of course I know but I don't have the time at the moment
you know there are examples inside the distribution, try a the regular upload and use it four times without using an array for the files (never used the multiupload in my own CMS systems)
Kill the googlebot
Posts: 6
36 credits Members referred : 0
« Reply #12 on: Nov 30, 2006, 05:30:27 PM »
i would prefer to use the multi upload incase i need to add other things, when will you have time to show me? i really need to get this working, but i dont know how, i was thinking maybe with a while statement that writes to the db but i wouldnt know how to set it up properly, so please help me soon as you get the chance..... im sure others would find this useful too
Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6280
38506 credits Members referred : 374
i would prefer to use the multi upload incase i need to add other things, when will you have time to show me? i really need to get this working, but i dont know how, i was thinking maybe with a while statement that writes to the db but i wouldnt know how to set it up properly, so please help me soon as you get the chance..... im sure others would find this useful too
I use the single upload with multiple files too, the point is that helping you is not done within a few minutes (since your problem is not related to this class), can't say when I have the time ... maybe next week or later ?