Database tmp name?

See all posts Reply

Database tmp name? new!
by David, 14 years ago
I've managed to get the values of a multiple upload submitted to a database, however instead of saving filename it is saving the temp name?

Any help would be great.Reply
Re: Database tmp name? new!
by colin, 14 years ago
You can read some class variables after calling process(). The following ones can be used to get the filename details:
$foo->file_dst_name
$foo->file_dst_name_body
$foo->file_dst_name_ext
$foo->file_dst_path
$foo->file_dst_pathname

It is all in the docs, and the FAQ.Reply
Re: Database tmp name? new!
by David, 14 years ago
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());
Reply
Re: Database tmp name? new!
by colin, 14 years ago
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
Re: Database tmp name? new!
by David, 14 years ago
I made one small change which was the following.

echo 'OK ' .$handle->file_dst_pathname;

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!Reply
Re: Database tmp name? new!
by colin, 14 years ago
It should be something like this:

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);
}
Reply
Re: Database tmp name? new!
by David, 14 years ago
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);
}
Reply
Re: Database tmp name? new!
by colin, 14 years ago
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());
Reply
Re: Database tmp name? new!
by David, 14 years ago
Sorry is this is a simple thing to get around, I’m getting an error with the code provided.

Fatal error: Class 'Upload' not found in /home/houses/public_html/upload/upload.php on line 29
Line 29: $handle = new Upload($file);
Reply
Re: Database tmp name? new!
by colin, 14 years ago
You need to make sure that the class file in included.
include('class.upload.php');
Reply
Re: Database tmp name? new!
by David, 14 years ago
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
Re: Database tmp name? new!
by David, 14 years ago
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
Re: Database tmp name? new!
by colin, 14 years ago
Not sure why. Are the 8 images the same? Are they the original versions? The resized versions? A mix of both?Reply
Re: Database tmp name? new!
by David, 14 years ago
If I upload 4 different images, its creating two reszied versions of each image.Reply
Re: Database tmp name? new!
by colin, 14 years ago
There can be many reason. The function process() should be called only four times. Does your script output any "OK"? How many?

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.Reply
Re: Database tmp name? new!
by David, 14 years ago
Yes it's outputting OKOKOKOK at the top when I have clicked upload.

Is this somthing to do with it?Reply
Re: Database tmp name? new!
by colin, 14 years ago
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
Re: Database tmp name? new!
by David, 14 years ago
I get the message OKOKOKOK, then I also get 4 blocks underneath stating file uploaded with success etc...

In total its displaying 4 ok along the top and also four file uploaded with success and the image names.Reply
Re: Database tmp name? new!
by colin, 14 years ago
So you have 4 files uploaded. There must be some old left over images in your directory maybe.Reply
Re: Database tmp name? new!
by David, 14 years ago
I've uploaded the same image in the four upload boxes available, the folder was empty on the hosting and this is the output:

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?Reply
Re: Database tmp name? new!
by colin, 14 years ago
There must be a problem in your code, or the way you call your script, etc... I cannot do much more with the information I have.

Try to add the following line, just before unset($handle);
echo $handle->log;
This will give you a lot of debugging information.Reply
Re: Database tmp name? new!
by David, 14 years ago
This is the information it output.

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
Reply
Re: Database tmp name? new!
by colin, 19 years, 10 months ago
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