28, May 2012

Renaming en uploading multiple files - webmaster forum

 
Webdigity webmaster forums
[ 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: Renaming en uploading multiple files
« previous next »
Pages: [1] 2 Print
Instabuck - The easy way to sell digital products online

Author Topic: Renaming en uploading multiple files  (Read 3692 times)
My name is Bong, James Bong
*
Gender: Male
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 Kiss

Can you help..
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #1 on: Aug 15, 2006, 08:10:36 am »


My name is Bong, James Bong
*
Gender: Male
Posts: 12
76 credits
Members referred : 0


« Reply #2 on: Aug 15, 2006, 10:39:02 am »

Hhhhmmmmm...... Huh

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: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #3 on: Aug 15, 2006, 11:04:19 am »

Hhhhmmmmm...... Huh

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: Male
Posts: 12
76 credits
Members referred : 0


« Reply #4 on: Aug 15, 2006, 11:13:24 am »

ok, i'm going to try....  Undecided
My name is Bong, James Bong
*
Gender: Male
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 Grin
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
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: Male
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: Male
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: Male
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.

http://www.benbboeken.nl/test
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #10 on: Aug 17, 2006, 08:19:11 am »

Hello,

try this form:
Code:
<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:
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) {
$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: Male
Posts: 12
76 credits
Members referred : 0


« Reply #11 on: Aug 17, 2006, 12:21:09 pm »

Thanx!! We're getting there  Grin

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 Angry
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: Male
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: Male
Posts: 12
76 credits
Members referred : 0


« Reply #13 on: Aug 18, 2006, 01:55:20 am »

I'm getting a little ashamed  Embarrassed , 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: Male
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>

<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>

My name is Bong, James Bong
*
Gender: Male
Posts: 12
76 credits
Members referred : 0


« Reply #15 on: Aug 18, 2006, 10:07:00 am »

Thnx! But now the files rename with timestamps again....
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #16 on: Aug 18, 2006, 11:21:51 am »

Thnx! But now the files rename with timestamps again....

change this value to "false":
$my_upload->rename_file = true; (didn't tested the code)

My name is Bong, James Bong
*
Gender: Male
Posts: 12
76 credits
Members referred : 0


« Reply #17 on: Aug 18, 2006, 11:40:59 am »

This way the script uploads the files without name-changing.........
(why do i have the feeling the solution is so near...)
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #18 on: Aug 18, 2006, 11:51:04 am »

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...

My name is Bong, James Bong
*
Gender: Male
Posts: 12
76 credits
Members referred : 0


« Reply #19 on: Aug 18, 2006, 03:10:41 pm »

great! Don't rush...i'm just glad you're still willing to look over it ;-)
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=3672
Tags : php html mysql xhtml databases Bookmark this thread : Digg Del.icio.us Dzone more....

Pages: [1] 2 Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP  >  PHP classes @finalwebsites.com  >  Easy PHP Upload (Moderator: Olaf)
Topic: Renaming en uploading multiple files
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 28, 2012, 03:04:56 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!






Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2012 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.