Jump to content

Image Upload Help


Guest

Recommended Posts

this is a general legist of my code

[code]
<form action="index.php?p=addhouse" method="POST" enctype="multipart/form-data">
<input type="file" name="imgfile" />
<input type="submit" name="Submit" value="Add House" />
</form>
[/code]

php form handeler

[code]
$_FILES = $HTTP_POST_FILES;
define ( 'UPLOAD_PATH', 'Uploads/Houses/' );
define ( 'MAX_SIZE', '50000' ); // define the max single file size (bytes)
$allow = array ( 'png', 'gif', 'jpg' ); // allowed types
$max_width = 640; // max width (resize width)
$max_height = 480; // max height (resize height)

$save = false; // save original upload image (bool true or false)
$thumb = 't_'; // the thunmbnail hyphen t_image_name.jpg

$quality = 100; // the quality for (jpg) images

$out = ''; // output container

$sqlimage1="";
$sqlimage2="";
$sqlimage3="";
$sqlimage4="";

//upload image 1

if ( is_uploaded_file ( $_FILES['image1']['tmp_name'] ) )
{
$fs = filesize ( $_FILES['image1']['tmp_name'] );
// file size test (size test)

if ( $fs > 10 && $fs <= MAX_SIZE )
{
$fn = strtolower ( $_FILES['image1']['name'] );
$fe = substr ( $fn, ( strrpos ( $fn, '.' ) + 1 ) );

// is it a valid image type (extension test)
if ( in_array ( $fe, $allow ) )
{
// is it a valid image type (image file test)
if ( ( $image = getimagesize ( $_FILES['image1']['tmp_name'] ) ) !== false )
{
// get the width and height of the upload image
list ( $width, $height ) = $image;
// load the upload image into the image resource

switch ( $fe )
{
case 'gif' :
$old = imagecreatefromgif ( $_FILES['image1']['tmp_name'] );
break;
case 'jpg' :
$old = imagecreatefromjpeg ( $_FILES['image1']['tmp_name'] );
break;
case 'png' :
$old = imagecreatefrompng ( $_FILES['image1']['tmp_name'] );
break;
}
// create the original image ratio
$original_ratio = ( $width / $height );
// figure what controls the resize (width or height)
if ( $max_width / $max_height > $original_ratio )
{
$max_width = ( $max_height * $original_ratio );
}
else
{
$max_height = ( $max_width / $original_ratio );
}

// create a new image resource the size of the new ratio
$new = imagecreatetruecolor ( $max_width, $max_height );
//copy original to the resized image and resample it
imagecopyresampled ( $new, $old, 0, 0, 0, 0, $max_width, $max_height, $width, $height );

// save the new thumbnail image
switch ( $fe )
{
case 'gif' :
imagegif ( $new, UPLOAD_PATH . $thumb . $fn );
break;
case 'jpg' :
imagejpeg ( $new, UPLOAD_PATH . $thumb . $fn, $quality);
break;
case 'png' :
imagepng ( $new, UPLOAD_PATH . $thumb . $fn );
break;
}

// kill the image resources

imagedestroy ( $old );
imagedestroy ( $new );

// are we saving the orignal

if ( $save ) {
@move_uploaded_file ( $_FILES['image1']['tmp_name'], UPLOAD_PATH . $sn );
}

$out = "Image 1 uploaded successfully";
$sqlimage1 = UPLOAD_PATH . $sn;
} else {
$out = 'Image 1 not a valid image file';
}
} else {
$out = 'Image 1 not in the allowed upload types -> (' . implode ( ', ', $allow ) . ')';
}
} else {
$out = 'Image 1 size greater than the allowed file size';
}
} else {
$out = 'Image 1 not a valid upload file';
}

echo $out;
[/code]

The problem is that out always says image 1 not a valid upload file even if it is valid i cannot figure out why can someone help
Link to comment
https://forums.phpfreaks.com/topic/27050-image-upload-help/
Share on other sites

Because in the form you have imgfile as the name attribute for file input, you should use $_FILES['imgfile']

Try correcting this and it should work for you.  If you need to allow more than one image to upload you can use html array by doing something like this...

<input type="file" name="imgfile[]" />

...on each input.  They will then be accessible as an array afterwards ie: $_FILES['imgfile'][[i]n[/i]] where [i]n[/i] is the number of the file starting at 0 for the first file.

HTH

Dest
Link to comment
https://forums.phpfreaks.com/topic/27050-image-upload-help/#findComment-123725
Share on other sites

Okay, try this...

Remove or comment out (put // infront of the line) this line...

[code]
$_FILES = $HTTP_POST_FILES;
[/code]

So that php does not read that line and try it again.  $_FILES is a superglobal in php versions after 4.3.0 (I believe, I could have the version number wrong from memory) so your line could be interfering with that...

Let me know,

Dest
Link to comment
https://forums.phpfreaks.com/topic/27050-image-upload-help/#findComment-123742
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.