image resize is giving me nightmares!


#1

Hi

I’m redoing arts of an existing code, and in that code theres the option to upload 10 images, like this:

[code]

Images:









[/code]

this calls for ‘addoffer.php’ where the images are handled as:

[php]

<?php if(isset($_POST[s1])) { if(!empty($_FILES[images][name][0])) { while(list($key,$value) = each($_FILES[images][name])) { if(!empty($value)) { $NewImageName = $t."_offer_".$value; copy($_FILES[images][tmp_name][$key], "re_images/".$NewImageName); $MyImages[] = $NewImageName; } } if(!empty($MyImages)) { $ImageStr = implode("|", $MyImages); } } ?>[/php]

What it does now is copy the files to ‘re_images’ and add a string to the DB where ‘|’ is the seperator (like image01|image02etc).
The problem is that it’ll store the images as supplied by the user (so there are 1.5MB files stored that will be used 150pixels wide)… and there is the problem popping up. I’ve tried to add all sorts of codes I found, at least 10 of them’ but I’m doing something terribly wrong as nothing seems to work.

Can anyone show me how it’s done PLEASE?
thanks in advance!

JP

**MOD EDIT - Added PHP Tags to for readablity! Please refer to http://phphelp.com/guidelines.php for posting guidelines


#2

I find that file uploading scripts are a perfect example of when to use a function (especially if you allow for multiple uploads at once). It allows for a great deal of modularity and teaches you a little bit of the basics of OOP (which is, if I may, a big player of the future of web and application development). So, I’m not going to look at your code, but give you somewhat of a plan of approach on how you could fix it yourself:

  • make a function in PHP that will upload the file
  • try it with a single upload at a time, using the function
  • add the loop for multiple uploads, calling the function on each iteration

If you can figure out how to upload files (and I presume you wrote the preceding code yourself), then I’m sure creating a function and calling it is a piece of cake :wink:


#3

thx Zyppora

but as I said I’m only redoing some of the code ;) I did not write this myself and since I’m posting this in the beginner section I kinda need more help than just a guidance to what I should do…

but still,

Thanks

JP


#4

Ah, sorry about that (musta missed it). In that case, are you getting any errors? And since you’re dealing with file uploads over 1MB (as you’ve stated ;)), are you allowed to upload files that big (see max_upload or something in php.ini).

As an alternative: try to contact the person who did write the code, and see if you can get any answers out of them :slight_smile: That usually sheds a light.


#5

geen probleem ;)

The person(s) that did the original coding are unknown to me, so that wont help. The script, as it’s running now does not give any errors, and yes I can upload 1MB files, and theres the problem. I need to mod this script so it either won’t accept large files or resizes the ones that are too big after upload. I can’t access the php.ini file with this host, unless I configure my own php.


#6

Ah, now we’re getting somewhere. You want to restrict uploading of files bigger than x kB/MB. I’d suggest the following two additions:

In your HTML form, add the following:

<input type="hidden" name="MAX_FILE_SIZE" value="x">

The value attribute represents the number of bytes allowed. However, this alone won’t secure your application (because you should NEVER EVER trust external input).

So, we’ll add the following to our script:
[php]
if ($_FILES[‘images’][‘size’][] > x) {
die (Your file is too big!);
}
[/php]

Please replace ‘x’ with the appropriate (and consistent) value of maximum allowed number of bytes per file.

For more information on handling file uploads, please see this page: Handling File Uploads.


#7

I’ve added the code you posted (no dount I did something wrong even there!), but the result is:

Parse error: syntax error, unexpected T_STRING in /home/.fitz/snewpers/re.heymans.name/AddOffer.php on line 5

the code now is:

[code]

Images:






















[/code]

and the PHP I added is:

[code]<?PHP

<? require_once("conn.php"); if ($_FILES['images']['size']> 512000) { die (Your file is too big!); } if(isset($_POST[s1])) { if(!empty($_FILES[images][name][0])) { while(list($key,$value) = each($_FILES[images][name])) { if(!empty($value)) { $NewImageName = $t."_offer_".$value; copy($_FILES[images][tmp_name][$key], "re_images/".$NewImageName); $MyImages[] = $NewImageName; } } if(!empty($MyImages)) { $ImageStr = implode("|", $MyImages); } } ... more here ?>[/code]

I’m doing something wrong eh?

thanks!


#8

Well, yes. You open PHP tags twice in the first and second line for example. Secondly, there’s no single or double quotes around the text in the die() statement (which I forgot, but you should have seen ;)). Thirdly, you should stick the filesize check in your loops, so it checks the filesizes of each of the files separately (not sure what it does now). And lastly, you should have changed the die() statement into something a little more graceful :wink:


#9

Thanks for your efforts Zyppora

but I didn’t get it ito work :confused: I’ve tried all kinds of things with the code you gave and it does upload, but only if the total sum of the files is smaller than 512000. It’ll show the die() message then.
This stuff is keeping me awake at nights :) (i

Thanks, I’ll have to keep trying…