Tags:
upload,
resize,
multiple,
multiple_upload_example,
foto_example,
combine
surveyman posted on 2009-02-02 18:19:35 #
Is there anyway to combine the multiple_upload_example and the foto_example to make a script that uploads 3 or more images and then resizes them to 600px or less. I can get both to work properly apart, but I am to new to PHP to know how to combine them to make the script I need. I have found many codes to do one or the other, but have yet to find a code that does both.
This site looks like it has the answer, but they are apart. Can anyone help me?
The most popular forum posts:
Comments / discussions
Olaf posted on 2009-02-03 06:10:10 #
There are several topic with the same question check this thread too:
http://www.finalwebsites.com/forums/topic/easy-upload-multi-file-rename
http://www.webdigity.com/index.php/topic,4563.0.Multiple+images+with+thumbnails.html
While the first link is about renaming files, the example show how-to use the class object inside a "foreach" loop. Please don't respond to the external forum
surveyman posted on 2009-02-03 15:35:39 #
Sorry if I sounded pushy. I searched the entire forum and couldn't find anything. I also clicked the links above and still do not understand completely. Remember I am new to PHP...like 1 week new. Thanks for the links anyways. I will try to decipher them with my limited knowledge.
Olaf posted on 2009-02-03 16:00:44 #
A muliple upload is not an easy job for a beginner, I suggest to start with a single upload (to learn) and read the php manual to understand uploads:
http://nl3.php.net/manual/en/features.file-upload.php
and check also the foreach function:
http://nl3.php.net/manual/en/control-structures.foreach.php
surveyman posted on 2009-02-16 18:10:54 #
Olaf. I have been doing a lot of research on the issue, but I still cannot get your two scripts to mingle together. Can you please post the correct code if it is possible. Your scripts work prefectly for me seperately, but together I get a heap of mess. I would appreciate it greatly. I have been trying since I last posted to no avail.
Olaf posted on 2009-02-16 18:44:18 #
@surveyman,
so you get nothing?
do you tried this one?
if (isset($_POST['Submit']) && $_POST['Submit'] == "Upload")
{
foreach ($_FILES as $key => $val)
{
$foto_upload->the_temp_file = $_FILES[$key]['tmp_name'];
$foto_upload->the_file = $val;
$foto_upload->http_error = $_FILES[$key]['error'];
$foto_upload->replace = "y";
$foto_upload->do_filename_check = "n";
if ($foto_upload->upload())
{
$foto_upload->process_image(false, true, true, 80);
$foto_upload->message[] = "Processed foto: ".$foto_upload->file_copy."!"; // "file_copy is the name of the foto"
}
}
}
surveyman posted on 2009-02-16 21:53:39 #
Thanks for the snippet.
Ok...this is what I got so far. I added that code to the foto_upload.php and it works fine, but it is not uploading different images. How do I get the upload form to see its 4 different upload boxes. After submitting the images I get the following:
File: us1.jpg successfully uploaded!
Processed foto: us1.jpg!
File: us1.jpg successfully uploaded!
File: us1.jpg successfully uploaded!
File: us1.jpg successfully uploaded!
I tried to rename the upload boxes to upload1 - upload4, but that didnt work out. I really appreciate the help and I hope I am not to much a bother. Thanks again.
Olaf posted on 2009-02-16 22:09:34 #
check the input element here
<input type="file" name="upload[]" size="30">
the square brackets will create an array in PHP
surveyman posted on 2009-02-16 22:39:03 #
I added the bracket to my upload files so that I have four upload files that look like:
<input type="file" name="upload[]" size="30">
Upon uploading I get the error:
Notice: Array to string conversion in /home/wickedw1/public_html/upload/upload_class.php on line 89
and this is below the form. Although I am uploading a jpg.
Only files with the following extensions are allowed: .jpg
Any suggestions?
Olaf posted on 2009-02-17 07:04:02 #
what is you current script? (don't post the classes here)
surveyman posted on 2009-02-17 14:58:24 #
What do you mean? Do you want me to post the foto_upload script here?
Olaf posted on 2009-02-17 15:37:37 #
Quote from: surveyman
"What do you mean? Do you want me to post the foto_upload script here?"
only your modified code (I know my own scripts/code) ;)
surveyman posted on 2009-02-17 16:18:14 #
I kept most of the default since like I said before I am a PHP noob but this is where I started editing the foto_upload script:
$max_size = 1024*1024; // the max. size for uploading (~1MB)
define("MAX_SIZE", $max_size);
$foto_upload = new Foto_upload;
$foto_upload->upload_dir = $_SERVER['DOCUMENT_ROOT']."/images/"; // "files" is the folder for the uploaded files (you have to create these folder)
$foto_upload->foto_folder = $_SERVER['DOCUMENT_ROOT']."/images/gallery/";
$foto_upload->thumb_folder = $_SERVER['DOCUMENT_ROOT']."/images/gallery/";
$foto_upload->extensions = array(".jpg"); // specify the allowed extension(s) here
$foto_upload->language = "en";
$foto_upload->x_max_size = 9000;
$foto_upload->y_max_size = 9000;
$foto_upload->x_max_thumb_size = 600;
$foto_upload->y_max_thumb_size = 600;
if (isset($_POST['Submit']) && $_POST['Submit'] == "Upload") {
foreach ($_FILES as $key => $val)
{
$foto_upload->the_temp_file = $_FILES['upload']['tmp_name'];
$foto_upload->the_file = $_FILES['upload']['name'];
$foto_upload->http_error = $_FILES['upload']['error'];
$foto_upload->replace = (isset($_POST['replace'])) ? $_POST['replace'] : "n"; // because only a checked checkboxes is true
$foto_upload->do_filename_check = "n";
if ($foto_upload->upload()) {
$foto_upload->process_image(false, true, true, 80);
$foto_upload->message[] = "Processed foto: ".$foto_upload->file_copy."!"; // "file_copy is the name of the foto"
echo '<img src="../images/gallery/'.$foto_upload- />file_copy.'">';
}
}
}
$error = $foto_upload->show_error_string();
?>
<!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>Photo-upload form</title>
<style type="text/css">
<!--
body {
text-align:center;
}
label {
margin:0;
float:left;
display:block;
width:120px;
}
#main {
width:350px;
margin:0 auto;
padding:20px 0;
text-align:left;
}
-->
</style>
</head>
<body>
<div id="main">
<h1>Photo-upload form</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_size; ?>"><br />
<div>
<label for="upload">Select a foto</label>
<input type="file" name="upload[]" size="35">
<input type="file" name="upload[]" size="35">
<input type="file" name="upload[]" size="35">
<input type="file" name="upload[]" size="35">
</div>
<div>
<label for="replace">Replace an old foto?</label>
<input type="checkbox" name="replace" value="y"></div>
<p style="margin-top:25px;text-align:center;"><input type="submit" name="Submit" id="Submit" value="Upload">
</p>
</form>
<p><?php echo $error; ?></p>
</div>
</body>
</html>
As you can see it is pretty much the default with the exception of what you told me yesterday.
Olaf posted on 2009-02-17 16:24:28 #
You didn't use these IMPORTANT rows of code:
$foto_upload->the_temp_file = $_FILES[$key]['tmp_name'];
$foto_upload->the_file = $val;
$foto_upload->http_error = $_FILES[$key]['error'];
replace this code with (the old)
$foto_upload->the_temp_file = $_FILES['upload']['tmp_name'];
$foto_upload->the_file = $_FILES['upload']['name'];
$foto_upload->http_error = $_FILES['upload']['error'];
surveyman posted on 2009-02-17 19:16:48 #
Now I have this:
if (isset($_POST['Submit']) && $_POST['Submit'] == "Upload") {
foreach ($_FILES as $key => $val)
{
$foto_upload->the_temp_file = $_FILES[$key]['tmp_name'];
$foto_upload->the_file = $val;
$foto_upload->http_error = $_FILES[$key]['error'];
$foto_upload->replace = (isset($_POST['replace'])) ? $_POST['replace'] : "n"; // because only a checked checkboxes is true
$foto_upload->do_filename_check = "n";
if ($foto_upload->upload()) {
$foto_upload->process_image(false, true, true, 80);
$foto_upload->message[] = "Processed foto: ".$foto_upload->file_copy."!"; // "file_copy is the name of the foto"
echo '<img src="../images/gallery/'.$foto_upload- />file_copy.'">';
}
}
}
I am still getting the error:
Notice: Array to string conversion in /home/wickedw1/public_html/upload/upload_class.php on line 89
and
Only files with the following extensions are allowed: .jpg
I am still uploading a jpg file. I know I am hassling you, but I really like the script and would love it to work with both.
Olaf posted on 2009-02-17 19:42:43 #
which one is row 89?
Only files with the following extensions are allowed: .jpg
I think this error is related to the first error
surveyman posted on 2009-02-17 21:41:48 #
Ok I did some research and I think that the
function check_file_name($the_name) {
if ($the_name != "") {
if (strlen($the_name) > $this->max_length_filename) {
$this->message[] = $this->error_text(13);
return false;
} else {
is causing the error. It looks to me that $the_name is being pulled as an array and not a string. Is there a way to get this to be pulled as a string?
Olaf posted on 2009-02-17 21:46:07 #
surveyman posted on 2009-02-17 21:50:20 #
row 89 is:
if (strlen($the_name) > $this->max_length_filename) {
i think the strlen is trying to convert $the_name to a string but it is coming in as an array.
Olaf posted on 2009-02-17 21:55:14 #
put an echo before this row an tell me what is the name:
echo $foto_upload->the_file = $val;
surveyman posted on 2009-02-17 21:59:31 #
I put that in the upload_class.php and the foto_upload.php. It didn't echo anything on either page.
Olaf posted on 2009-02-17 22:25:02 #
Okay that is the working code:
if (isset($_POST['Submit']) && $_POST['Submit'] == "Upload") {
$numfiles = 4; // enter the number of file fields here
for ($i = 0; $i < $numfiles; $i++) {
if (!empty($_FILES['upload']['tmp_name'][$i])) {
$foto_upload->the_temp_file = $_FILES['upload']['tmp_name'][$i];
$foto_upload->the_file = $_FILES['upload']['name'][$i];
$foto_upload->http_error = $_FILES['upload']['error'][$i];
$foto_upload->replace = (isset($_POST['replace'])) ? $_POST['replace'] : "n"; // because only a checked checkboxes is true
$foto_upload->do_filename_check = "n";
if ($foto_upload->upload()) {
$foto_upload->process_image(false, true, true, 80);
$foto_upload->message[] = 'Processed foto: '.$foto_upload->file_copy.'!<br />
<img src="../images/gallery/'.$foto_upload- />file_copy.'"><br /><br />';
}
}
}
}
surveyman posted on 2009-02-17 22:37:27 #
It works flawlessly now. Olaf..you are great and have been a wonderful help. I will definitely be adding a link to your site / forums on some of my sites. Thanks again...and I hope this little experience will help others as well.
Olaf posted on 2009-02-17 22:40:25 #
You're welcome!
Actually I have plans to write a new version for php5 (I hope I will find the time this year) :(
surveyman posted on 2009-02-17 22:41:28 #
Your script is great and the only one I used that I liked. Trust I tried about 100 different scripts and it came down to yours and this little addition you just made.
Olaf posted on 2009-02-17 22:59:05 #
That was one of the reason I wrote that script and many others...
many upload scripts are very complicated, this one is okay but it could be better (it was my first class script ever)
surveyman posted on 2009-03-13 14:54:30 #
Your script is working wonderfully...but I seem to have run into another issue. This issue has nothing to do with the image script, but I was wondering can your script upload mp3s? I have a friend that want to put mp3s on their site. I know how to get the song link to go into the mySQL database, but I was wondering how to get a mp3 (that I referenced in the mySQL database) to go to a certain folder in the directory.
I figured since you helped me out before I could come back here ask you before I search the entire internet for the solution again.
Thanks.
Olaf posted on 2009-03-13 14:56:22 #
please start a new thread...