Jump to content

[SOLVED] cannot see image


squiblo

Recommended Posts

ok, what i am doing is, uploading an image and then using it as a profile image, i have successfully uploaded an image and the image is now in the database, but when i try getting the image from the database and trying to view it, the image is not being shown but area where the image should be is marked with the currupt file icon. i do not understand what could be wrong, here are all my scripts being used:

 

upload.php

<?php

session_start();

include("checklogin.php");

$_SESSION['myusername'];

$username = $_SESSION['myusername'];

if ($_POST['submit'])
{

//get file attributes
$name = $_FILES['myfile']['name'];
$tmp_name = $_FILES['myfile']['tmp_name'];

if ($name)
{
    //start upload process

$location = dirname(__FILE__)."/profileimages/$name";
        move_uploaded_file($tmp_name,$location);
        $query = mysql_query("UPDATE members SET imagelocation='$location' WHERE username='$username'") or die(mysql_error());

die("Your profile picture have been upload! <a href='view.php'>View</a>");



}
else
die("Please select a file!");




}

echo "Welcome, ".ucwords(strtolower($_SESSION['myusername']))."!<p>";

echo "Upload your image:

<form action='upload.php' method='POST' enctype='multipart/form-data'>
  File: <input type='file' name='myfile'> <input type='submit' name='submit' value='Upload'>
</form>

";

?>

 

checklogin.php

<?php
$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name=""; // Database name 
$tbl_name="members"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

if(isset($_POST['Login'])){
// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
//$_SESSION['myusername']=$dbusername;

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "profile.php"
session_register("myusername");
session_register("mypassword"); 
header("location:profile.php");
}
else {
header("location: http://www.squiblo.com/retrylogin.php");
}
}
?>

 

view.php

<?php

session_start();

include("checklogin.php");

$username = $_SESSION['myusername'];

$query = mysql_query("SELECT * FROM members WHERE username='$username'");
if (mysql_num_rows($query)==0)
   die("User not found!");
else
{

$row = mysql_fetch_assoc($query);
$location = $row['imagelocation'];
echo "<img src ='$location' width='700' height='700'>";
}


?>

Link to comment
Share on other sites

when i echo out my file location i get:

/customers/squiblo.com/squiblo.com/httpd.www/profileimages/homepage-pic.jpeg 

 

the reason i think i get this long code is becuase im using a sharing hosting website (one.com), or am i being misleaded?

Link to comment
Share on other sites

check two things

 

1) what is the location that is in the database

2) is the file actually at the location that you want it to be in ?

 

I am a little hazy on file uploads, but I do not understand this part

 

<?php
//get file attributes
$name = $_FILES['myfile']['name'];
$tmp_name = $_FILES['myfile']['tmp_name'];  //  <----------- where is 'tmp_name' coming from ??

// and then later
move_uploaded_file($tmp_name,$location);  // <------- are you sure it is moving the file where you want it to be ?

Link to comment
Share on other sites

this is the location of the file shown in the database:

/customers/squiblo.com/squiblo.com/httpd.www/profileimages/Autumn Leaves.jpg

 

i got help with this script so i also do not understand parts, im quiet new to php.

 

the process is, upload the image (in my case to ftp) > then get image from ftp to sql > then show image from sql on webpage. (i think)

Link to comment
Share on other sites

i think the problem lies here

<?php
$location = dirname(__FILE__)."/profileimages/$name";

 

from here http://us2.php.net/manual/en/function.dirname.php  it looks like this will get the pathname of the page that is currently loaded, or "/customers/squiblo.com/squiblo.com/httpd.www".  You then append the path that you do want to the end of this, bringing you to the monstrosity in your database.

 

do you happen to know what the correct path of the file SHOULD be ? perhaps then we can see exactly where the problem is, but right now I do not know what your expected directory structure is

Link to comment
Share on other sites

try just opening "www.domain.com/profileimages/(name of the image)" from your browser and see if it shows up properly.

 

If it does, I would suggest taking out the dirname(__FILE__) from your $location variable.

If it doesn't, you need to check what the actual path to the file is

Link to comment
Share on other sites

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.