class.upload.php is a powerful and mature PHP class to manage uploaded files, and manipulate images in many ways. The script is available under a GPL license.
Thank you for prompt reply; this is my very first attempt at anything like this. It currently looks like this? Is it something obvious I have missed or is this just wrong? Where would I attach the $foo? Your help is greatly appreciated.
include('class.upload.php');
$files = array();
foreach ($_FILES['my_field'] as $k => $l) {
foreach ($l as $i => $v) {
if (!array_key_exists($i, $files))
$files[$i] = array();
$files[$i][$k] = $v;
}
}
foreach ($files as $file) {
$handle = new Upload($file);
if ($handle->uploaded) {
$handle->Process("./");
if ($handle->processed) {
echo 'OK';
} else {
echo 'Error: ' . $handle->error;
}
} else {
echo 'Error: ' . $handle->error;
}
unset($handle);
}
// now, you have all your picture names in $uploaded
// do your database insert like this for instance
$sql = "INSERT INTO photos2 (my_field1, my_field2, my_field3, my_field4) ";
$sql .= "VALUES ('".$my_field[0]."', '".$my_field[1]."', '".$my_field[2]."', '".$my_field[3]."')";
$x = mysql_query($sql) or die(mysql_error());
You need to save the image names in the database where it says echo 'OK';. There, you can do your SQL query. But in your SQL, you use $handle->file_dst_pathname for instance.Reply
include('class.upload.php');
$files = array();
foreach ($_FILES['my_field'] as $k => $l) {
foreach ($l as $i => $v) {
if (!array_key_exists($i, $files))
$files[$i] = array();
$files[$i][$k] = $v;
}
}
foreach ($files as $file) {
$handle = new Upload($file);
if ($handle->uploaded) {
$handle->Process("./");
if ($handle->processed) {
// it is only here you know that your upload has succeeded
// so you can insert the filename in your database here
$sql = "INSERT INTO photos (filename) ";
$sql .= "VALUES ('".mysql_real_escape_string($handle->file_dst_pathname)."')";
$x = mysql_query($sql) or die(mysql_error());
} else {
echo 'Error: ' . $handle->error;
}
} else {
echo 'Error: ' . $handle->error;
}
unset($handle);
}
Ok. It is now filling the name in correctly on the database, however I am uploading 4 and its inserting the same name in the 4 fields?
My code...
include('class.upload.php');
$files = array();
foreach ($_FILES['my_field'] as $k => $l) {
foreach ($l as $i => $v) {
if (!array_key_exists($i, $files))
$files[$i] = array();
$files[$i][$k] = $v;
}
}
foreach ($files as $file) {
$handle = new Upload($file);
if ($handle->uploaded) {
$handle->Process("./");
if ($handle->processed) {
// it is only here you know that your upload has succeeded
// so you can insert the filename in your database here
$sql = "INSERT INTO photos2 (my_field1, my_field2, my_field3, my_field4) ";
$sql .= "VALUES ('".mysql_real_escape_string($handle->file_dst_pathname)."', '".mysql_real_escape_string($handle->file_dst_pathname)."', '".mysql_real_escape_string($handle->file_dst_pathname)."', '".mysql_real_escape_string($handle->file_dst_pathname)."')";
$x = mysql_query($sql) or die(mysql_error());
} else {
echo 'Error: ' . $handle->error;
}
} else {
echo 'Error: ' . $handle->error;
}
unset($handle);
}
This is basic PHP, we are starting to get out of topic in this forum...
Your code should look like this then:
include('class.upload.php');
// create an array to hold your filnames
$images = array();
$files = array();
foreach ($_FILES['my_field'] as $k => $l) {
foreach ($l as $i => $v) {
if (!array_key_exists($i, $files))
$files[$i] = array();
$files[$i][$k] = $v;
}
}
foreach ($files as $file) {
$handle = new Upload($file);
if ($handle->uploaded) {
$handle->Process("./");
if ($handle->processed) {
echo 'OK';
// add the filename to the array
$images[] = $handle->file_dst_pathname;
} else {
echo 'Error: ' . $handle->error;
}
} else {
echo 'Error: ' . $handle->error;
}
unset($handle);
}
// you can insert the filenames in your database here,
// from the array in which you stocked them
// but you will need to do some error checking, in case of the upload didn't work
$sql = "INSERT INTO photos2 (my_field1, my_field2, my_field3, my_field4) ";
$sql .= "VALUES ('".mysql_real_escape_string($images[0])."', '".mysql_real_escape_string($images[1])."', '".mysql_real_escape_string($images[2])."', '".mysql_real_escape_string($images[3])."')";
$x = mysql_query($sql) or die(mysql_error());
I had just noticed a second before your reply... thank you for your help! I will make a donation very soon! Couldnt have done it without your help.Reply
I'm sure this is somthing totally different, but since using that code its now producing two of each image on the hosting? So when I upload 4 images its creating 8?Reply
So it means that the class has been called only 4 times, as it should.
I don't know why you have 8 images in the end. Have you tried to delete the 8 images before running the script again? How many are actually created when you run it?Reply
OKsystem information - class version : 0.30 - operating system : Linux - PHP version : 5.2.14 - GD version : 2.0.34 - supported image types : png jpg gif bmp - open_basedir : no restriction - upload_max_filesize : 64M (67108864 bytes) - language : en_GB source is an uploaded file - upload OK - file name OK determining MIME type - Checking MIME type with Fileinfo PECL extension Fileinfo PECL extension not available - Checking MIME type with UNIX file() command MIME type detected as image/jpeg by UNIX file() command - MIME validated as image/jpeg source variables - You can use all these before calling process() file_src_name : 1house.jpg file_src_name_body : 1house file_src_name_ext : jpg file_src_pathname : /tmp/phpGPAtjw file_src_mime : image/jpeg file_src_size : 91083 (max= 67108864) file_src_error : 0 - source file is an image image_src_x : 536 image_src_y : 309 image_src_pixels : 165624 image_src_type : jpg image_src_bits : 8 process file to ./test/ - file size OK - file mime OK : image/jpeg - file name safe format - destination variables file_dst_path : ./test/ file_dst_name_body : 1house file_dst_name_ext : jpg - image operation, keep extension - checking for auto_rename - destination file details file_dst_name : 1house.jpg file_dst_pathname : ./test/1house.jpg - 1house.jpg doesn't exist already - image resizing or conversion wanted - source image is JPEG - setting destination file type to jpg - resizing... check x/y sizes resized image object created image_src_x y : 536 x 309 image_dst_x y : 500 x 288 - converting... fills in transparency with default color - saving image... JPEG image created image objects destroyed - process OK OKsystem information - class version : 0.30 - operating system : Linux - PHP version : 5.2.14 - GD version : 2.0.34 - supported image types : png jpg gif bmp - open_basedir : no restriction - upload_max_filesize : 64M (67108864 bytes) - language : en_GB source is an uploaded file - upload OK - file name OK determining MIME type - Checking MIME type with Fileinfo PECL extension Fileinfo PECL extension not available - Checking MIME type with UNIX file() command MIME type detected as image/jpeg by UNIX file() command - MIME validated as image/jpeg source variables - You can use all these before calling process() file_src_name : 1house.jpg file_src_name_body : 1house file_src_name_ext : jpg file_src_pathname : /tmp/php3bYALR file_src_mime : image/jpeg file_src_size : 91083 (max= 67108864) file_src_error : 0 - source file is an image image_src_x : 536 image_src_y : 309 image_src_pixels : 165624 image_src_type : jpg image_src_bits : 8 process file to ./test/ - file size OK - file mime OK : image/jpeg - file name safe format - destination variables file_dst_path : ./test/ file_dst_name_body : 1house file_dst_name_ext : jpg - image operation, keep extension - checking for auto_rename auto_rename to 1house_1.jpg - destination file details file_dst_name : 1house_1.jpg file_dst_pathname : ./test/1house_1.jpg - 1house_1.jpg doesn't exist already - image resizing or conversion wanted - source image is JPEG - setting destination file type to jpg - resizing... check x/y sizes resized image object created image_src_x y : 536 x 309 image_dst_x y : 500 x 288 - converting... fills in transparency with default color - saving image... JPEG image created image objects destroyed - process OK OKsystem information - class version : 0.30 - operating system : Linux - PHP version : 5.2.14 - GD version : 2.0.34 - supported image types : png jpg gif bmp - open_basedir : no restriction - upload_max_filesize : 64M (67108864 bytes) - language : en_GB source is an uploaded file - upload OK - file name OK determining MIME type - Checking MIME type with Fileinfo PECL extension Fileinfo PECL extension not available - Checking MIME type with UNIX file() command MIME type detected as image/jpeg by UNIX file() command - MIME validated as image/jpeg source variables - You can use all these before calling process() file_src_name : 1house.jpg file_src_name_body : 1house file_src_name_ext : jpg file_src_pathname : /tmp/phptdE34W file_src_mime : image/jpeg file_src_size : 91083 (max= 67108864) file_src_error : 0 - source file is an image image_src_x : 536 image_src_y : 309 image_src_pixels : 165624 image_src_type : jpg image_src_bits : 8 process file to ./test/ - file size OK - file mime OK : image/jpeg - file name safe format - destination variables file_dst_path : ./test/ file_dst_name_body : 1house file_dst_name_ext : jpg - image operation, keep extension - checking for auto_rename auto_rename to 1house_2.jpg - destination file details file_dst_name : 1house_2.jpg file_dst_pathname : ./test/1house_2.jpg - 1house_2.jpg doesn't exist already - image resizing or conversion wanted - source image is JPEG - setting destination file type to jpg - resizing... check x/y sizes resized image object created image_src_x y : 536 x 309 image_dst_x y : 500 x 288 - converting... fills in transparency with default color - saving image... JPEG image created image objects destroyed - process OK OKsystem information - class version : 0.30 - operating system : Linux - PHP version : 5.2.14 - GD version : 2.0.34 - supported image types : png jpg gif bmp - open_basedir : no restriction - upload_max_filesize : 64M (67108864 bytes) - language : en_GB source is an uploaded file - upload OK - file name OK determining MIME type - Checking MIME type with Fileinfo PECL extension Fileinfo PECL extension not available - Checking MIME type with UNIX file() command MIME type detected as image/jpeg by UNIX file() command - MIME validated as image/jpeg source variables - You can use all these before calling process() file_src_name : 1house.jpg file_src_name_body : 1house file_src_name_ext : jpg file_src_pathname : /tmp/phpxVpFaY file_src_mime : image/jpeg file_src_size : 91083 (max= 67108864) file_src_error : 0 - source file is an image image_src_x : 536 image_src_y : 309 image_src_pixels : 165624 image_src_type : jpg image_src_bits : 8 process file to ./test/ - file size OK - file mime OK : image/jpeg - file name safe format - destination variables file_dst_path : ./test/ file_dst_name_body : 1house file_dst_name_ext : jpg - image operation, keep extension - checking for auto_rename auto_rename to 1house_3.jpg - destination file details file_dst_name : 1house_3.jpg file_dst_pathname : ./test/1house_3.jpg - 1house_3.jpg doesn't exist already - image resizing or conversion wanted - source image is JPEG - setting destination file type to jpg - resizing... check x/y sizes resized image object created image_src_x y : 536 x 309 image_dst_x y : 500 x 288 - converting... fills in transparency with default color - saving image... JPEG image created image objects destroyed - process OKReply
You must be running the script twice. As you can see in the log, it creates only four images, the last one being file_dst_pathname : ./test/1house_3.jpgReply
Any help would be great.
It is all in the docs, and the FAQ.
Is that what you mean?
ps. I dont mind making a small donation for your time, providing we get this thing to work, it has me confused!
My code...
Your code should look like this then:
Fatal error: Class 'Upload' not found in /home/houses/public_html/upload/upload.php on line 29
Line 29: $handle = new Upload($file);
You need to debug the code. Add some echo ... in your code, within your loop, etc... to see why it ends up creating too many images.
Is this somthing to do with it?
I don't know why you have 8 images in the end. Have you tried to delete the 8 images before running the script again? How many are actually created when you run it?
In total its displaying 4 ok along the top and also four file uploaded with success and the image names.
1house.jpg
1house_1.jpg
1house_2.jpg
1house_3.jpg
1house_4.jpg
1house_5.jpg
1house_6.jpg
1house_7.jpg
With only four upload slots, its managed to create 8 images?
Try to add the following line, just before unset($handle);
This will give you a lot of debugging information.
OKsystem information
- class version : 0.30
- operating system : Linux
- PHP version : 5.2.14
- GD version : 2.0.34
- supported image types : png jpg gif bmp
- open_basedir : no restriction
- upload_max_filesize : 64M (67108864 bytes)
- language : en_GB
source is an uploaded file
- upload OK
- file name OK
determining MIME type
- Checking MIME type with Fileinfo PECL extension
Fileinfo PECL extension not available
- Checking MIME type with UNIX file() command
MIME type detected as image/jpeg by UNIX file() command
- MIME validated as image/jpeg
source variables
- You can use all these before calling process()
file_src_name : 1house.jpg
file_src_name_body : 1house
file_src_name_ext : jpg
file_src_pathname : /tmp/phpGPAtjw
file_src_mime : image/jpeg
file_src_size : 91083 (max= 67108864)
file_src_error : 0
- source file is an image
image_src_x : 536
image_src_y : 309
image_src_pixels : 165624
image_src_type : jpg
image_src_bits : 8
process file to ./test/
- file size OK
- file mime OK : image/jpeg
- file name safe format
- destination variables
file_dst_path : ./test/
file_dst_name_body : 1house
file_dst_name_ext : jpg
- image operation, keep extension
- checking for auto_rename
- destination file details
file_dst_name : 1house.jpg
file_dst_pathname : ./test/1house.jpg
- 1house.jpg doesn't exist already
- image resizing or conversion wanted
- source image is JPEG
- setting destination file type to jpg
- resizing...
check x/y sizes
resized image object created
image_src_x y : 536 x 309
image_dst_x y : 500 x 288
- converting...
fills in transparency with default color
- saving image...
JPEG image created
image objects destroyed
- process OK
OKsystem information
- class version : 0.30
- operating system : Linux
- PHP version : 5.2.14
- GD version : 2.0.34
- supported image types : png jpg gif bmp
- open_basedir : no restriction
- upload_max_filesize : 64M (67108864 bytes)
- language : en_GB
source is an uploaded file
- upload OK
- file name OK
determining MIME type
- Checking MIME type with Fileinfo PECL extension
Fileinfo PECL extension not available
- Checking MIME type with UNIX file() command
MIME type detected as image/jpeg by UNIX file() command
- MIME validated as image/jpeg
source variables
- You can use all these before calling process()
file_src_name : 1house.jpg
file_src_name_body : 1house
file_src_name_ext : jpg
file_src_pathname : /tmp/php3bYALR
file_src_mime : image/jpeg
file_src_size : 91083 (max= 67108864)
file_src_error : 0
- source file is an image
image_src_x : 536
image_src_y : 309
image_src_pixels : 165624
image_src_type : jpg
image_src_bits : 8
process file to ./test/
- file size OK
- file mime OK : image/jpeg
- file name safe format
- destination variables
file_dst_path : ./test/
file_dst_name_body : 1house
file_dst_name_ext : jpg
- image operation, keep extension
- checking for auto_rename
auto_rename to 1house_1.jpg
- destination file details
file_dst_name : 1house_1.jpg
file_dst_pathname : ./test/1house_1.jpg
- 1house_1.jpg doesn't exist already
- image resizing or conversion wanted
- source image is JPEG
- setting destination file type to jpg
- resizing...
check x/y sizes
resized image object created
image_src_x y : 536 x 309
image_dst_x y : 500 x 288
- converting...
fills in transparency with default color
- saving image...
JPEG image created
image objects destroyed
- process OK
OKsystem information
- class version : 0.30
- operating system : Linux
- PHP version : 5.2.14
- GD version : 2.0.34
- supported image types : png jpg gif bmp
- open_basedir : no restriction
- upload_max_filesize : 64M (67108864 bytes)
- language : en_GB
source is an uploaded file
- upload OK
- file name OK
determining MIME type
- Checking MIME type with Fileinfo PECL extension
Fileinfo PECL extension not available
- Checking MIME type with UNIX file() command
MIME type detected as image/jpeg by UNIX file() command
- MIME validated as image/jpeg
source variables
- You can use all these before calling process()
file_src_name : 1house.jpg
file_src_name_body : 1house
file_src_name_ext : jpg
file_src_pathname : /tmp/phptdE34W
file_src_mime : image/jpeg
file_src_size : 91083 (max= 67108864)
file_src_error : 0
- source file is an image
image_src_x : 536
image_src_y : 309
image_src_pixels : 165624
image_src_type : jpg
image_src_bits : 8
process file to ./test/
- file size OK
- file mime OK : image/jpeg
- file name safe format
- destination variables
file_dst_path : ./test/
file_dst_name_body : 1house
file_dst_name_ext : jpg
- image operation, keep extension
- checking for auto_rename
auto_rename to 1house_2.jpg
- destination file details
file_dst_name : 1house_2.jpg
file_dst_pathname : ./test/1house_2.jpg
- 1house_2.jpg doesn't exist already
- image resizing or conversion wanted
- source image is JPEG
- setting destination file type to jpg
- resizing...
check x/y sizes
resized image object created
image_src_x y : 536 x 309
image_dst_x y : 500 x 288
- converting...
fills in transparency with default color
- saving image...
JPEG image created
image objects destroyed
- process OK
OKsystem information
- class version : 0.30
- operating system : Linux
- PHP version : 5.2.14
- GD version : 2.0.34
- supported image types : png jpg gif bmp
- open_basedir : no restriction
- upload_max_filesize : 64M (67108864 bytes)
- language : en_GB
source is an uploaded file
- upload OK
- file name OK
determining MIME type
- Checking MIME type with Fileinfo PECL extension
Fileinfo PECL extension not available
- Checking MIME type with UNIX file() command
MIME type detected as image/jpeg by UNIX file() command
- MIME validated as image/jpeg
source variables
- You can use all these before calling process()
file_src_name : 1house.jpg
file_src_name_body : 1house
file_src_name_ext : jpg
file_src_pathname : /tmp/phpxVpFaY
file_src_mime : image/jpeg
file_src_size : 91083 (max= 67108864)
file_src_error : 0
- source file is an image
image_src_x : 536
image_src_y : 309
image_src_pixels : 165624
image_src_type : jpg
image_src_bits : 8
process file to ./test/
- file size OK
- file mime OK : image/jpeg
- file name safe format
- destination variables
file_dst_path : ./test/
file_dst_name_body : 1house
file_dst_name_ext : jpg
- image operation, keep extension
- checking for auto_rename
auto_rename to 1house_3.jpg
- destination file details
file_dst_name : 1house_3.jpg
file_dst_pathname : ./test/1house_3.jpg
- 1house_3.jpg doesn't exist already
- image resizing or conversion wanted
- source image is JPEG
- setting destination file type to jpg
- resizing...
check x/y sizes
resized image object created
image_src_x y : 536 x 309
image_dst_x y : 500 x 288
- converting...
fills in transparency with default color
- saving image...
JPEG image created
image objects destroyed
- process OK