Page 1 of 1

PHP Image Uploads and Access

Posted: Sat Mar 06, 2004 6:37 pm
by smith55js
I'm having problems accessing an image after I've uploaded it. Can anyone help me out?

Here's the code to upload the image::
umask(0000);
$uploadpath = '/home/boxelder/public_html/ads/';
$source = $HTTP_POST_FILES['file1']['tmp_name'];
$dest = '';

if ( ($source != 'none') && ($source != '' )) {

$imagesize = getimagesize($source);

switch ( $imagesize[2] ) {

case 0:

echo '<BR> Image is unknown <BR>';
break;

case 1:
echo '<BR> Image is a GIF <BR>';
$dest1 = uniqid(0).'.gif';
$dest = $uploadpath.$dest1;
$sql="UPDATE ads SET logo='$dest1' WHERE name='$name'";
$result=mysql_query($sql) or die(mysql_error());
break;

case 2:
echo '<BR> Image is a JPG <BR>';
$dest1 = uniqid(0).'.jpg';
$dest = $uploadpath.$dest1;
$sql="UPDATE ads SET logo='$dest1' WHERE name='$name'";
$result=mysql_query($sql) or die(mysql_error());
break;

case 3:
echo '<BR> Image is a PNG <BR>';
$dest1 = uniqid(0).'.png';
$dest = $uploadpath.$dest1;
$sql="UPDATE ads SET logo='$dest1' WHERE name='$name'";
$result=mysql_query($sql) or die(mysql_error());
break;

}

if ( $dest != '' ) {
chmod($source,0777);
if ( move_uploaded_file( $source, $dest ) ) {

echo 'File successfully stored.<BR>';


} else {

echo 'File could not be stored.<BR>';

}

}

} else {

echo 'File not supplied, or file too big.<BR>';

}
//************************
umask(0022);

So, the name of the uniqid image uploads correctly. The problem is here:

$sql="SELECT * FROM ads";
$result=mysql_query($sql);
$rows=mysql_num_rows($result);
$count = $num % $rows;
for($i=0; $i<$count; $i++)
$data=mysql_fetch_object($result);
$data=mysql_fetch_object($result);

if($data->logo){
echo "\n\n<img src=\"./ads/$data->logo\" alt=\"$data->name Image\" width=\"140\" align=\"center\">\n\n";


Basically, when you view the HTML source, you see nothing of the <img tag in the source, but you get a bunch of blank lines from the \n's in the echo statement. Can anyone tell me why this isn't working?? Another thing that might be a clue, is that the image's can't be viewed inside a web browser, but CAN be downloaded via FTP and then can be viewed in a web browser.

help...

Posted: Mon Mar 08, 2004 11:30 am
by mark-s
are you missing one or more concatenation operators from the last line?

Posted: Mon Mar 08, 2004 11:45 am
by liljester
im not sure what your trying to do ...

$num doesnt have any value that i can see... in the following line:
$count = $num % $rows;
$count will be 0 everytime, because the modulus of 0 ($num) divided by any number will always be 0... your for loop will not run

Posted: Mon Mar 08, 2004 11:48 am
by liljester
try this

Code: Select all

$sql="SELECT * FROM ads"; 
$result=mysql_query($sql); 
$rows=mysql_num_rows($result); 

while($data = mysql_fetch_object($result)){
    if($data->logo){ 
        echo "\n\n<img src="./ads/$data->logo" alt="$data->name Image" width="140" align="center">\n\n";
    }
}