Member Avatar

Hi, I decided to try and upload images by storing their path in a database, but have run into an error when displaying the images. The form works and the folder is populated whenever I run the script. I have tried a lot of solutions from similar threads, but to no avail. My view.php is;

<?php
include 'connect.php';
$query = "SELECT * FROM upload2 ";
$result = mysql_query($query) or die('Error, query failed');
$num = mysql_fetch_array($result);
$path = $row['path'];

if ($num > 0){
	echo "<p>There is an image in here!;</p>";
						
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
		echo "<img src='$path' />";
	}								
}

?>

Can anyone help?

Member Avatar

> The form works and the folder is populated whenever I run the script

What form? Which folder?

Look at view source in the browser and get the src data.

Does it refer to the correct folder? Sounds unlikely.

Member Avatar

Are you saying my view code is right?

When i use my upload from and it echoes "folder updated", the image appears in the 'upload' directory that i set it to.

I will check the path in browser

Member Avatar

Ok, so I typed the path into the browser and get nothing returned. My upload script is;

<?php
$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;
}

include 'connect.php';

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

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

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

mysql_close($connect);

echo "<br>Files uploaded<br>";
echo "<a href='form.html'>upload another?</a>";

}
?>

If i type in www.mysite.com/upload/image1.jpg i can see the image. Can anyone offer any advice?

Member Avatar

Check:

1) DB is updated
2) path for display in src


>If i type in www.mysite.com/upload/image1.jpg i can see the image. Can anyone offer any advice?

Your src should read "/upload/image1.jpg" or "upload/image1.jpg" if you are accessing from public root. If not, your db filepath data may be wrong.

Member Avatar

Ardav, thanks for taking the time to help me out with this problem.

The form update to the database fine, the path uploaded is "./upload/image1.jpg", its clear that the '.' is causing the issue, however, if I remove that from my upload script, I get an error and nothing is uploaded. This is the first time i have attempted to do this process and am a bit lost

Member Avatar
<?php
[B]$uploadDir = '/upload/';[/B]

if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
 
$filePath = $uploadDir . $fileName;
 
[B]$result = move_uploaded_file($tmpName, "." . $filePath);[/B]
if (!$result) {
echo "Error uploading file";
exit;
}
 
include 'connect.php';
 
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
 
$query = "INSERT INTO upload2 (name, size, type, path ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";
 
mysql_query($query) or die('Error, query failed : ' . mysql_error());
 
mysql_close($connect);
 
echo "<br>Files uploaded<br>";
echo "<a href='form.html'>upload another?</a>";
 
}
?>

OK I tried to keep your code intact, so bold lines for changes. Hope it works.

In your first script
where is the array

$row['path'];

(line 6)

come from?

Member Avatar

Ardav, your changes worked and the image uploaded without the '.',the image is going into the right folder and the file path is correct. When trying to view the image, i get a blank screen. I am currently trying to figure out why.

$row['path'];

Is from the table in the database. There are 5 columns; id, name, type, size and path.

Member Avatar

@pzuurveen i see what you mean, i have edited the code from array to rows;

<?php
include 'connect.php';
$query = "SELECT * FROM upload2 ";
$result = mysql_query($query) or die('Error, query failed');
$num = mysql_num_rows($result);
$path = $row['path'];

if ($num > 0){
echo "<p>There is an image in here!;</p>";
						
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
	echo $row['path']; 
	echo "<img src='$path' />";
								}								
							}

?>

I'm echoed the path to ensure it was correct

at line 6

Row['path']

still doesn't exist
you create it at line 11

#while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){

when php has an error like that u often get a blank screen
to show errors: start your script with

error_reporting(E_ALL);
Member Avatar

Thank you thank you pzuurveen! That little bit of script was all I needed to debug the issue. I wasn't aware of that before. For those that are interested, my completed script is;

include 'connect.php';
error_reporting(E_ALL);
$query = "SELECT * FROM upload2 ";
$result = mysql_query($query) or die('Error, query failed');
$num = mysql_num_rows($result);

if ($num > 0){
echo "<p>There is an image in here!;</p>";
						
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo "<img src='$row[path]' />";
}							
}

You will have to include you connection details mind!

Thanks again for the help and advice.