Jump to content

[SOLVED] problem displaying images with php and mysql


Recommended Posts

I am uploading images to a server and saving the script location to a mysql database.  It is loading into the server. When I call it with PHP it doesn't show up.  When I try to access it directly I

get a an error stating that this directory is restricted without an index

file.  I added an index file, but it made no difference.  The directory is set

to 777.  Also, I manually changed the chmod on the image to 777 and now I am

getting a "page not found" error.

 

here is the upload code:

 

<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload"
value=" Upload "></td>
</tr>
</table>
</form>


<?php
$db_host = "xxxxxx";
$db_user = "xxxxx";
$db_pwd = "xxxxx";
$db_name = "comment";

$uploadDir = 'upload/';

if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}


if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);

$query = "INSERT INTO upload2 (name, size, type, path ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";

mysql_query($query) or die('Error, query failed : ' . mysql_error());


echo "<br>Files uploaded<br>";

}
?>

 

Here is the display code:

<?php
$db_host = "xxxxxx";
$db_user = "xxxxx";
$db_pwd = "xxxxx";
$db_name = "comment";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
?>
<img src="banner-top.gif"/>
<table>
<?php
$sql = "SELECT * FROM upload2";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>".$row['userfile']."</td>";
echo "<td><img src=".$row['path']."/></td>";
echo "</tr>";
}
?>

 

I can't seem to find the problem.  Thanks

		mysql_select_db($db_name) or die(mysql_error());
		$results = mysql_query($sql) or die(mysql_error());
		$row = mysql_fetch_array($results) or die(mysql_error());

 

try that in place of

 

$query = mysql_query($sql);
while($row = mysql_fetch_array($query))

 

		mysql_select_db($db_name) or die(mysql_error());
		$results = mysql_query($sql) or die(mysql_error());
		$row = mysql_fetch_array($results) or die(mysql_error());

 

 

Thanks for the tip, however I am still getting the same problem with no error from mysql. 

try chmoding to 775 rather than 777. I have found in various cases this works for me.

 

It could also be an issue with the server security. If you're set to something like 3.1 security that can be a big change from high security.

 

 

I changed the directory to 775 and had to change each photo to 775 to be able to view them be typing in http://site/directory, however I am still not able to call the images with PHP into the index page.  Also, how can I make sure the image is uploaded as a 775? 

The path was corrupted due to the fact it was adding "/" to the end of the path:

[code=php:0]
echo "<td><img src=".$row['path']."/></td>";

[/code]

 

Resulting in:

http://site.com/directory/image.jpg/

 

I removed the slash for now and it is working perfectly  ;D

 

How do you include the "/" in the "<img />" tag to keep it xhtml strict?

 

thanks for everybody's help

if(isset($_POST['upload']))
{

$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
chmod($filePath, 0755);
if (!$result) {
echo "Error uploading file";
exit;
}


if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
} 
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);

$query = "INSERT INTO upload2 (name, size, type, path ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";

mysql_query($query) or die('Error, query failed : ' . mysql_error()); 

include '../library/closedb.php';

echo "<br>Files uploaded<br>";

yes I did...it did not work...however I cleared my cache, went back and tried it:

 

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
chmod($filePath, 0755);
if (!$result) {
echo "Error uploading file";
exit;
}

 

It worked great, thanks for your help, I really really appreciate it.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.