Trouble with multi file uploads

Thank you. Here’s one. I’m trying to figure out how I can add more than more than one image at a time. I’m having trouble figuring out where to start to alter this. As it is now, when you add an image you can only select one at a time. I can’t think of a way to add multiple images at one time. This one gives me a headache. I’m thinking it’s a script someplace.
[php]<?php

include “…/sql.php”;
if ($_SESSION[‘admin’] != 1)
{
header(“Location: /admin”);
die();
}

$title = “Manage Images”;
$showSide = false;
include “…/header.php”;
include “…/carpreview.php”;

$car_id = intval($_GET[‘id’]);
$x = mysql_query(“select * from cars where id=” . $car_id);

$car = mysql_fetch_array($x, MYSQL_ASSOC);
if (!$car) die(“This vehicle no longer exists.”);

if (intval($_GET[‘main’]) > 0)
{
mysql_query(“update images set main=0 where car_id=” . $car_id);
mysql_query(“update images set main=1 where car_id=” . $car_id . " and id=" . intval($_GET[‘main’]));
}
elseif (intval($_GET[‘delete’]) > 0)
{
$del = intval($_GET[‘delete’]);
mysql_query(“delete from images where id=” . $del);
@unlink("…/images/vehicles/" . $car_id . “-” . $del . “.jpg”);
@unlink("…/images/vehicles/" . $car_id . “-” . $del . “t.jpg”);
}

showCarPreview($car);

$x = mysql_query(“select id,main from images where car_id=” . $car_id);
while ($y = mysql_fetch_array($x, MYSQL_ASSOC))
{
?>




<?=$y['main'] == "1" ? "MAIN  " : ""?><input type=“radio” name=“main” value="<?=$y['id']?>" <?=$y['main'] == "1" ? "checked" : ""?> onclick=“javascript:location=‘carpics.php?id=<?=$car_id?>&main=<?=$y['id']?>’;”>
<?php if ($y['main'] != "1") { ?>  
X
<?php } ?>


<?php
}

?>

Add another image ...

<?php include "../footer.php"; ?>[/php]

Well, first, you are using an old version of PHP since you are using “deprecated” versions of MySQL. You need to
update the entire site to at least MySQLi (“improved” version) or to the more current standard of PDO. You can
use the “multiple” attribute in the input line. Which version of PHP are you using? Which version of HTML are you
coding for? HTML5 supports the multiple attribute. You would just add it into your input line something like:
This allows your form to accept multiple inputs.
The user can hold down the cntrl key and select multiple images to upload. But, you will need to alter your file
uploading code to handle the extra filenames. You did not show us the PHP’s upload code for the files. That will
needed to be changed.

NOTE: The “multiple” attribute is new as of HTML5 and is not available for older versions. (As far as I know!)
There are other ways to load multiple files, but, this is the easy way…

Absolutely. I didn’t write this. This was done years ago. php 5 is set on the server. This is a temp fix until I can get an entire new website rolled out.

You are right I forgot to include this file.
[php]<?php

include “…/sql.php”;
if ($_SESSION[‘admin’] != 1)
{
header(“Location: /admin”);
die();
}

$file = $_FILES[‘Filedata’][‘name’];
$tmpfile = $_FILES[‘Filedata’][‘tmp_name’];

$name = md5(microtime());
$ext = strtolower(end(split(".", $file)));

if ($ext == “png”)
$img = imagecreatefrompng($tmpfile) or die(“failed”);
elseif ($ext == “gif”)
$img = imagecreatefromgif($tmpfile) or die(“failed”);
else
$img = imagecreatefromjpeg($tmpfile) or die(“failed”);

$width = imageSX($img);
$height = imageSY($img);

for ($i = 0; $i <= 1; $i++)
{
if ($i == 1)
{
//$target_width = 750;
$target_width = 640;
$target_height = ceil($target_width * 3/4);
}
else
{
$target_width = 105;
$target_height = ceil($target_width * 3/4);
}

if ($width > $target_width || $height > $target_height)
{
$tr = $target_width/$target_height;
$ir = $width/$height;

if ($ir > $tr) // too wide
	$target_height = $target_width / $ir;
else // too tall
	$target_width = $target_height * $ir;

}
else
{
$target_width = $width;
$target_height = $height;
}

if ($i == 0)
{
$car_id = intval($_POST[‘car_id’]);
mysql_query(“insert into images (car_id) values (” . $car_id . “)”);
$img_id = mysql_insert_id();
}

$target_dir = “…/images/vehicles”;
//mkdir($target_dir, 0777, true);

$new_img = ImageCreateTrueColor($target_width, $target_height);
imagecopyresampled($new_img, $img, 0, 0, 0, 0, $target_width, $target_height, $width, $height);
imagejpeg($new_img, $target_dir . “/” . $car_id . “-” . $img_id . ($i == 0 ? “t” : “”) . “.jpg”, 90);
//die($target_dir . “/” . $car_id . “-” . $img_id . ($i == 0 ? “t” : “”) . “.jpg”);
}

unlink($_FILES[‘Filedata’][‘tmp_name’]);

if (end(mysql_fetch_array(mysql_query(“select count(*) from images where main=1 and car_id=” . $car_id), MYSQL_ASSOC)) == 0)
mysql_query(“update images set main=1 where car_id=” . $car_id . " limit 1");

header(“Location: carpics.php?id=” . $car_id);
?>
[/php]

Okay, AAnderson, let’s step back a bit… You are trying to fix an old site temporarily until the new one goes live.
Is the new one still going to be hosted under PHP5 ? Current version is PHP7 and a lot of changes were made
thru the various versions in between. The new one should use at least PHP5.5 or the newest PHP7. Now, since
you are not in need of updating the current one to MySQLi or better PDO, we should just fix the multiple upload
issue only.

The problem is not the PHP, but, the HTML. Are you using the current standard HTML5? You can tell this by the
tags at the top of the HTML section of the page. If the HTML tag is just and there is no DOCTYPE tag,
then most likely it is HTML5. If it has something like this:

Then, it is most likely not HTML5. Either way, you can test it and just add the MULTIPLE attribute and see if it throws an error to you. Here is a link that shows you how to do multiple uploads. You will see a sample of the code in the post that explains it in detail. (No need to duplicate that post here...) Hope this helps! [url=http://stackoverflow.com/questions/2704314/multiple-file-upload-in-php]http://stackoverflow.com/questions/2704314/multiple-file-upload-in-php[/url]

Yes. That is correct. The site is hosted on godaddy. Which believe it or not is still only php 5.3. I don’t know when they plan on changing it. The client is not going to switch his hosting plan. I asked because I’m not found of godaddy myself. I think it’s a bunch of nonsense. Thank for your help. I appreciate it.

Well, did you read my note? Perhaps I was not clear. First, the PHP issues… So, Godaddy has a control panel
where you can set your PHP version. As far as I know, it only goes up to PHP5.4… But, for uploading files, it is
not a PHP issues, but, a HTML issue. Let’s review file uploading so you understand it…

In your HTML page, you have a form that has the input tag for the type=file. This allows you to select one file to
be uploaded to your website. If you are using HTML5, you can set an option on this field tag for multiple files. IF
and only IF you have a web page using HTML5, this will allow the user to select more than one file to upload. It is
done like any other Windows editor using the CNTRL key to select a second, third, 23rd file or using the SHIFT key
to select a group of files in a folder. (Pressing the first file, pressing SHIFT and selecting the last file you want to
send.) Now, this is just done in the browser and the filename(s) will show inside the field tag where it shows
the BROWSE button and field input area. Once you press the submit button, the browser sends the file to the
server and then the PHP code takes over.

Files are loaded to the server’s temporary folder and the PHP code must move the file to it’s location where you
want it to be saved to. On less than HTML5, you can only send one file at a time. You can add multiple inputs
for the files using just a list of fields with different names. ( file1, file2, file3 ) If you know how many files the
user might send, you can do it that way. If, let’s say, you set a limit of five files to upload, you can list five of
the input fields one below the next for each of the possible files. This will work on any HTML version. You can
also set up some sort of Javascript to keep a list of files and use a displayed version of these. Instead of just
one file input, it would allow the user to add more to the list. As they select a file, it adds it to the grand list and
let’s them add another one. Any of these would work.

Now, on the PHP code in the server, once the user pressed submit, you need to read all of the inputs and if they
are not empty, copy the files to where you want them saved to. Your PHP code you last posted had code in
it from line#10 to line#72 that takes the temporary file uploaded and creates the pix and thumbnail and uses
the results. If you add five uploads, you would have to make this section of your code into a loop to go thru
all of the files uploaded. (Not much different code than is there now.)

Hope all that makes sense to you. I may have over-explained it, but, want you to understand it all. Now, to pick
a way to handle this, you need to give us an idea of how many files would be the max you want to allow to be
uploaded. If it is 3 or 5 or other small number, it would be fastest to just list them and allow the user to enter
them as needed. If you need to allow 20, then, you would need a more complex code script to sort it out.
Let us know your limits on this and we can help you with it…

Yes. I read the note. The entire website are all php files. Every page is php. The home page is index.php with header.php and footer.php included with a css stylesheet. There are no html pages.

LOL, Sorry, perhaps I am not clear again… ANY page you view hold HTML. So, this code:

<?PHP Some PHP code... ?> ... page headers Body of page... NOTE: this is the HTML part...

That is a file that MUST have the extension on the server of .php ! It is a combined page with PHP code (which
is called a script) AND the HTML part for the brower. The PHP is completed server-side only and it’s output is
dumped into the HTML stream and then is sent to the browser. You never see any PHP in a webpage that is in
a browser. (Such as the page you see now. This site is written in PHP and HTML, but, if you view-source this
page, you will never see any PHP code listed. Unless it is in a post which is not executable.)

So, in your file with an extension of .php, you should see both PHP and HTML mixed together. The level of the
HTML is determined by the DOCTYPE and the tags. If it is HTML5, it just has a simple or
tag and nothing else. Look at your pages and you can see which version of HTML you have. Let us know and
also let us know the max uploads you want to allow per user. Then, we can sort it out for you…

Oh, most likely your HTML tag is in the header.php file or just after the top PHP scripts…

Personally, I would not spend the effort to extend the current functionality of this site, if you are actively working on redoing it, spend your effort on that. Now, if there is a functional issue that is causing the site not to operate correctly, by all means, fix it, but do not add features, such as multiple file uploads, to an old site.

Spend your effort on the design and building the new site, only work on the old if something breaks and needs immediate attention.

XHTML 1.0. So it’s not html 5. I see what you are saying. Perhaps you’re right I’ll talk the guy into letting these features go. And work on the new site.

Sponsor our Newsletter | Privacy Policy | Terms of Service