Forum Moderators: coopster

Message Too Old, No Replies

Reading images (files) in PHP

         

shamboo

8:51 pm on Jun 23, 2005 (gmt 0)

10+ Year Member



HI guys i just joined this forum .i hope some body could help me solve my problem.

Iam developing an appication regarding storing of images in mysql database in binary format. During reading iam getting the following error.
$fileHandle = fopen($fileUpload, "rb");
$fileContent = fread($fileHandle, filesize($fileUpload));

$fileContent = addslashes($fileContent);

The following is the error iam getting

fread(): supplied argument is not a valid stream resource in /var/www/grabfile.php on line 5

Some body please help me.
THanks

Burner

9:04 pm on Jun 23, 2005 (gmt 0)

10+ Year Member



I think it's got to be $fileUpload.

You could test by:
if (file_exists($fileUpload)) {
<code>;
}

If the procedure doesn't fire off then the file wasn't present.

Burner

Burner

9:11 pm on Jun 23, 2005 (gmt 0)

10+ Year Member



Another thought, Was that file uploaded by a POST operation in your browser?

I'll assume it is and then you could test it like this:

if ($_FILES['fileUpload']['error'] == 0) {
<code>
}

and it would already be in a temporary file as: $_FILES['fileUpload']['tmp_name']);

Burner

shamboo

8:06 pm on Jun 24, 2005 (gmt 0)

10+ Year Member



Thank you very much burner

i got it the correct code follows like this

//Declaration of global variables
global $strDesc;
global $fileUpload_name;
global $fileUpload_tmpname;
global $fileUpload_size;
global $fileUpload_type;
global $file_ff;
global $file_fw;
global $file_tumor;

$strDesc = $HTTP_POST_VARS["strDesc"];

//print_r($_FILES);

$fileUpload_name = $_FILES['fileUpload']['name'] . "<br>";
$fileUpload_tmpname = $_FILES['fileUpload']['tmp_name'] . "<br>";
$fileUpload_size = $_FILES['fileUpload']['size'] . "<br>";
$fileUpload_type = $_FILES['fileUpload']['type'] . "<br>";

and the fread and fopen statements are corrected as following

$fileHandle = fopen($_FILES['fileUpload']['tmp_name'], "rb");

$fileContent = fread($fileHandle, $_FILES['fileUpload']['size']);

$fileContent = addslashes($fileContent);

THanks

Burner

8:15 pm on Jun 24, 2005 (gmt 0)

10+ Year Member



Anytime,

I was wondering though, I don't know your app but have you considered uploading the files to a writeable directory and just putting the path to the file in the MySQL? It keeps the MySQL meaner, trimmer, faster, and makes automated data backups a heck of a lot easier later especially if you have scads of pictures.

Just wondering,
Burner

jatar_k

8:20 pm on Jun 24, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I agree with Burner, images in the db is nasty

remember when allowing people to upload anything to your site that you make absolutely sure what it is they're sending and only allow the proper files.

It would be unfortunate for them to upload an executable or soemthing else nasty and to have it viewable to them or to put it into your db.

Burner

8:46 pm on Jun 24, 2005 (gmt 0)

10+ Year Member



Thanks jatar_k,

I was actually debating with myself over whether I should have brought up some of the moral/ethical debates here. (Not your morals/ethics Shamboo) Some ISPs will absolutely not tolerate anything but text if you use a shared MySQL plan and consider it bad form. Additionally there's almost always eventually someone who tries uploading things you wouldn't expect, just to see what happens. The larger the site, the more likely someone will try to damage you.

If you backup your MySQL database through some type of browser script, you could be waiting hours to complete. Graphic files get HUGE when you're storing them. Better to leave 'em on a hard drive IMHO.

Burner