Dynadot โ€” .com Registration $8.99

[REP | PHP | NP$] Errors

Spaceship Spaceship
Watch
Impact
12
Hi,

If you give me an answer and it works you wil get some NP$ and rep.

I made a script in a tutorial which is supposed to take the image, give it a url and that. I go to view the image,
PHP:
?img=1
and i get loads of rubbish. Here are my codes:
PHP:
<? 
############################### 
# DB CONNECTION 
# CHANGE THESE VALUES 
############################### 
$dbcnx = mysql_connect("localhost", "root", ""); 
mysql_select_db("base64imgdb"); 

$path = "/toes"; 
$dir_handle = opendir($path) or die("Unable to open directory $path"); 

while ($file = readdir($dir_handle)) { 
$filetyp = substr($file, -3); 
if ($filetyp == 'gif' OR $filetyp == 'jpg') { 
$handle = fopen($file,'r'); 
$file_content = fread($handle,filesize($file)); 
fclose($handle); 
$encoded = chunk_split(base64_encode($file_content)); 
$sql = "INSERT INTO images SET sixfourdata='$encoded'"; 
mysql_query($sql); 
} 
} 

closedir($dir_handle); 
echo("complete"); 
mysql_close($dbcnx); 
?>
That was readdir.php which reads the directory for all the images.

PHP:
<? 
$dbcnx = mysql_connect("localhost", "root", ""); 

mysql_select_db("base64imgdb"); 

$img = $_REQUEST["img"]; 

$result = mysql_query("SELECT * FROM images WHERE imgid=" . $img . ""); 
if (!$result) { 
echo("<b>Error performing query: " . mysql_error() . "</b>"); 
exit(); 
} 
while ($row = mysql_fetch_array($result) ) { 
$imgid = $row["imgid"]; 
$encodeddata = $row["sixfourdata"]; 
} 
mysql_close($dbcnx); 
echo base64_decode($encodeddata); 
?>[/img]

That was image.php which assigns the [php]?img=here

view.php which shows the image:
PHP:
<img src="http://localhost/Shop/pro/image.php?img=2">

Im not sure why it displays loads of symbols, maybe it goes wrong somewhere??

Thanks rep given.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
PHP:
<?php

###############################
# DB CONNECTION
# CHANGE THESE VALUES
###############################
$dbcnx = mysql_connect("localhost", "root", "");
mysql_select_db("base64imgdb");

$path = "/toes";
$dir_handle = opendir($path) or die("Unable to open directory $path");

while($file = readdir($dir_handle))
{
  $filetyp = substr($file, -3);
  if($filetyp == 'gif' || $filetyp == 'jpg')
  {
    $handle = fopen($file, 'r');
    $file_content = fread($handle, filesize($file));
    fclose($handle);
    $encoded = chunk_split(base64_encode($file_content));
    //Have it store the extension aswell, to be used in image.php to determine
    //content type for the header call. Like:
    $sql = "INSERT INTO images SET sixfourdata='$encoded', ext = '$filetyp'";
    mysql_query($sql);
  }
}
closedir($dir_handle);

echo("complete");

mysql_close($dbcnx);

?>

image.php
PHP:
<?php

$dbcnx = mysql_connect("localhost", "root", "");
mysql_select_db("base64imgdb");

$img = intval($_REQUEST['img']);

if(empty($img))
{
  exit;
}

$result = mysql_query("SELECT * FROM images WHERE imgid=".$img);

if(!$result)
{
  echo "<b>Error performing query: ".mysql_error()."</b>";
  exit;
}

//Only pulling data for one image, no need for the loop
/*
while($row = mysql_fetch_array($result))
{
  $imgid = $row["imgid"];
  $ext = $row['ext'];
  $encodeddata = $row["sixfourdata"];
}
*/
$row = mysql_fetch_array($result);
$ext = $row['ext'];
$encodeddata = $row['sixfourdata'];

switch($ext)
{
  case 'jpg':
  case 'jpeg':
       $content_type = 'image/jpeg';
       break;
  case 'png':
       $content_type = 'image/png';
       break;
  //etc
}

header("Content-type: $content_type");
echo base64_decode($encodeddata);

?>
 
Last edited:
0
•••
Thanks, you sorta made it store the extension but i need to be able to tell each image by ?img=1 etc.

I know get this error:
HTML:
Notice: Undefined index: ext in e:\program files\easyphp1-8\www\shop\pro\image.php on line 31

Notice: Undefined variable: content_type in e:\program files\easyphp1-8\www\shop\pro\image.php on line 46

Warning: Cannot modify header information - headers already sent by (output started at e:\program files\easyphp1-8\www\shop\pro\image.php:31) in e:\program files\easyphp1-8\www\shop\pro\image.php on line 46
 
Last edited:
0
•••
Surely someone can help me, where are you all guys?
 
0
•••
If you need to tell each image by 1 or 2 then also store the extension in the table in a new column. On view.php get the extension from the database for that particular file and then do other processes. :D
 
0
•••
I can help you webmonkey if you send me the entire php file. If not, sorry.
 
0
•••
Im still having the problem :(
 
0
•••
You need to include a header ;)

I don't know the extention of the file, so I can't tell you witch. Just use:
header('Content-type: image/EXTENTION');
 
0
•••
what if i have multiple file type and also, where do i put that?
 
0
•••
switch($extension) {
case "gif": header('Content-type: image/gif'); break;
case "jpg": case "jpeg": header('Content-type: image/jpeg'); break;
case "png": header('Content-type: image/png'); break;
case "bmp": header('Content-type: image/bmp'); break;
}
 
0
•••
I get this when i run readdir above:
Error said:
Warning: opendir(E:\Program Files\EasyPHP1-8\www\Shop\pro oes): failed to open dir: Invalid argument in e:\program files\easyphp1-8\www\shop\pro\readdir.php on line 11
Unable to open directory E:\Program Files\EasyPHP1-8\www\Shop\pro oes
 
0
•••
Your directory is "/toes", on a windows machine it'll read as \toes. PHP sees that as a tab "\t"
 
0
•••
I have changed the directory to pics and this is what i get:
PHP:
Warning: fopen(Picture2.jpg): failed to open stream: No such file or directory in e:\program files\easyphp1-8\www\shop\pro\readdir.php on line 18

Warning: filesize(): Stat failed for Picture2.jpg (errno=2 - No such file or directory) in e:\program files\easyphp1-8\www\shop\pro\readdir.php on line 19

Warning: fread(): supplied argument is not a valid stream resource in e:\program files\easyphp1-8\www\shop\pro\readdir.php on line 19

Warning: fclose(): supplied argument is not a valid stream resource in e:\program files\easyphp1-8\www\shop\pro\readdir.php on line 20
complete

There is a file in there, so here is my readir:
PHP:
<?php 

############################### 
# DB CONNECTION 
# CHANGE THESE VALUES 
############################### 
$dbcnx = mysql_connect("localhost", "root", ""); 
mysql_select_db("base64imgdb"); 

$path = ".\pics"; 
$dir_handle = opendir($path) or die("Unable to open directory $path"); 

while($file = readdir($dir_handle)) 
{ 
  $filetyp = substr($file, -3); 
  if($filetyp == 'gif' || $filetyp == 'jpg') 
  { 
    $handle = fopen($file, 'r'); 
    $file_content = fread($handle, filesize($file)); 
    fclose($handle); 
    $encoded = chunk_split(base64_encode($file_content)); 
    //Have it store the extension aswell, to be used in image.php to determine 
    //content type for the header call. Like: 
    $sql = "INSERT INTO images SET sixfourdata='$encoded', ext = '$filetyp'"; 
    mysql_query($sql); 
  } 
} 
closedir($dir_handle); 

echo("complete"); 

mysql_close($dbcnx); 

?>

Ill give rep where needed.
 
0
•••
0
•••
ok. i still hving probs
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Appraise.net
Unstoppable Domains
Domain Recover
DomainEasy โ€” Zero Commission
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back