11, October 2008

Multiple file upload & storing filenames - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP  >  PHP classes @finalwebsites.com  >  Easy PHP Upload (Moderator: Olaf)
Topic: Multiple file upload & storing filenames
« previous next »
Pages: [1] Print

Author Topic: Multiple file upload & storing filenames  (Read 2257 times)
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: Male
Posts: 6349
38918 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


Last blog : Upload images for usage in TinyMCE
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: Male
Posts: 6349
38918 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?


Last blog : Upload images for usage in TinyMCE
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: Male
Posts: 6349
38918 credits
Members referred : 374


It's time to use PHP5!


« Reply #5 on: Nov 28, 2006, 06:16:01 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.

do you have 4 db records and 4 images or 1 db record and 4 images?


Last blog : Upload images for usage in TinyMCE
Kill the googlebot
*
Posts: 6
36 credits
Members referred : 0


« Reply #6 on: Nov 29, 2006, 04:09:52 PM »

1 db, 4 rows in a table, this is an example of how id do the sql

$img01 = `" . $_FILES[upload][name][0] . "`;
$img02 = `" . $_FILES[upload][name][1] . "`;
$img03 = `" . $_FILES[upload][name][2] . "`;
$img04 = `" . $_FILES[upload][name][3] . "`;

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: Male
Posts: 6349
38918 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:

$img01 = `" . $_FILES[upload][name1] . "`;
$img02 = `" . $_FILES[upload][name2] . "`;
$img03 = `" . $_FILES[upload][name3]. "`;
$img04 = `" . $_FILES[upload][name4] . "`;


Last blog : Upload images for usage in TinyMCE
Kill the googlebot
*
Posts: 6
36 credits
Members referred : 0


« Reply #8 on: Nov 29, 2006, 06:40:28 PM »

cause i dont understand the source for that part too well, ill post the entire code and hopfully you can tell me how to do it

Code:
<?php
error_reporting
(E_ALL);
$username $usercook;
$password $passcook;
setcookie("usercook""$username");
setcookie("passcook""$password");

require(
"functions.php");
global 
$myrow;

if(
authenticate($username$password1)) {


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());

if(isset(
$_POST['Submit'])) {
$multi_upload->tmp_names_array $_FILES['upload']['tmp_name'];
$multi_upload->names_array $_FILES['upload']['name'];
$multi_upload->error_array $_FILES['upload']['error'];
$multi_upload->replace "y";
$multi_upload->upload_multi_files();


$img01 = `" . $_FILES[upload][name][0] . "`;
$img02 = `" . $_FILES[upload][name][1] . "`;
$img03 = `" . $_FILES[upload][name][2] . "`;
$img04 = `" . $_FILES[upload][name][3] . "`;

mysql_query("UPDATE members SET img01 = \"$img01\", img02 = \"$img02\", img03 = \"$img03\", img04 = \"$img04\" WHERE username = \"$username\");
}
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Upload Images</title>

<style type="text/css">
<!--
label {
width: 80px;
}
input {
margin-bottom:3px;
margin-left:5px;
}
-->
</style>
</head>

<body>

<p>Max. filesize = <?php echo $max_size?> bytes. (each) </p>
<form name="form1" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_size?>">
  <label for="upload[]">Image 1:</label>
  <input type="file" name="upload[]" size="30"><br>
  <label for="upload[]">Image 2:</label>
  <input type="file" name="upload[]" size="30"><br>
  <label for="upload[]">Image 3:</label>
  <input type="file" name="upload[]" size="30"><br>
  <label for="upload[]">Image 4:</label>
  <input type="file" name="upload[]" size="30"><br>
  <input type="hidden" username="<? echo("$myrow[username]"); ?>">
  <input type="submit" name="Submit" value="Submit">
</form>
<p><?php echo $multi_upload->show_error_string(); ?></p>
</body>
</html>
<? include("footer.php"); ?>
« Last Edit: Nov 29, 2006, 06:46:44 PM by darkdemon »
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6349
38918 credits
Members referred : 374


It's time to use PHP5!


« Reply #9 on: Nov 30, 2006, 12:26:21 AM »

access the file names this way:

$_FILES[upload][0][name]

hope that helps


Last blog : Upload images for usage in TinyMCE
Kill the googlebot
*
Posts: 6
36 credits
Members referred : 0


« Reply #10 on: Nov 30, 2006, 04:37:45 PM »

no it didnt, you dont know what or how i should go about doing this?  Cry
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6349
38918 credits
Members referred : 374


It's time to use PHP5!


« Reply #11 on: Nov 30, 2006, 05:24:18 PM »

no it didnt, you dont know what or how i should go about doing this?  Cry
Of course I know but I don't have the time at the moment Smiley

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)


Last blog : Upload images for usage in TinyMCE
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: Male
Posts: 6349
38918 credits
Members referred : 374


It's time to use PHP5!


« Reply #13 on: Nov 30, 2006, 06:12:02 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
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 ?


Last blog : Upload images for usage in TinyMCE
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=3015
Tags : php javascript html java databases Bookmark this thread : Digg Del.icio.us Dzone more....

Topic sponsors:
Get a permanent link here for $1.99!


Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP  >  PHP classes @finalwebsites.com  >  Easy PHP Upload (Moderator: Olaf)
Topic: Multiple file upload & storing filenames
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Oct 11, 2008, 01:57:25 PM





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!


Forum Statistics
Total Posts: 36.904
Total Topics: 7.557
Total Members: 4.148
Tutorials : 56
Resources : 143
Designs : 220
Latest Member: ljay

26 Guests, 4 Users online :

14 users online today:


HumanWorks Network
Technology news