i've already searched but i got more confused when new codes appears in my sight again (cuz im just a newbie)

So, what I want to is, when the user successfully uploads the image(upload.php), it will be viewed before continuing into the next step (view.php).

index.php

<html>

<body bgcolor="black" style="color:gray">
<form action="index.php" method=get>
<h1 align="center" style="color:gray" >Welcome to this simple application</h1>

<?php
//This will start a session
session_start();

$username = isset($_SESSION['username']);
$password = isset($_SESSION['password']);
$imagelocation = isset($_GET['imagelocation']);

//Check do we have username and password
if(!$username && !$password){
echo "Welcome Guest! <br> <a href=login.php>Login</a> | <a href=register.php>Register</a>";
}
else{

$connect = mysql_connect("localhost", "root", "");
if(!$connect){
die(mysql_error());
}
$select_db = mysql_select_db("test", $connect);
if(!$select_db){
die(mysql_error());
}

echo "Welcome ".$username." (<a href=logout.php>Logout</a>)<br />papakta ng web ang dpat ifillup bago mag homepage. pedeng may skip.<br />";

if ($imagelocation==NULL){
echo "<a href=upload.php>1.UPLOAD an IMAGE</a>";
}
}
?> 
</html>

upload.php

<?php

include("connect.php");

$username= isset($_SESSION['username']);

if (isset($_POST['submit']))
{
//get file attributes
$name=$_FILES['myfile']['name'];
$tmp_name= $_FILES['myfile']['tmp_name'];

if ($name)
{
//start now

$location = "avatars/$name";

move_uploaded_file($tmp_name,$location);

$query = mysql_query("UPDATE users SET imagelocation='$location' WHERE username='$username'");

die("Your avatar has been uploaded! <a href='view.php'>HOME</a>");

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

echo "Welcome, ".$username."!<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>";


?>

view.php

<?php

include("connect.php");

$username = $_SESSION['username'];
$imagelocation = isset($_SESSION['imagelocation']);

$query=mysql_query("SELECT * FROM users WHERE imagelocation='$imagelocation'");
if (mysql_num_rows($query)==0)
die("User not found! ");
else
{
$row = mysql_fetch_assoc($query);
$location = "avatars/$name";

echo "<img src='$location' width='100' height='100'>";
}

?>

However, when the user uploads a picture, then it will be proceed to view.php, the picture doesn't show up.

I have a folder named avatars, and when the user uploads, the picture is there.

I'm really thankful for those who'll helped me.

Recommended Answers

All 7 Replies

Where is your avatars folder relative to the root folder? If the file is there, it sounds like the problem is as simple as getting the path correct in your img tag.

hmmm I though I spotted you issue, but those sessions are odd, not sure what is going on with $imagelocation.

Where is your avatars folder relative to the root folder? If the file is there, it sounds like the problem is as simple as getting the path correct in your img tag.

it's there
(xampp/htdocs/test/test001/avatars/)

hmmm I though I spotted you issue, but those sessions are odd, not sure what is going on with $imagelocation.

actually i got irritated that's why i almost screwed up all and don't really know what i was doing so... yeah, about the $imagelocation, whenever i don't call that, it will have a warning that's why i said $imagelocation = isset($_GET) even though i know that i didn't declare anything about $_GET.

looks like you need to change your upload script from scratch then. Tizag has a good script. I would post a solution, but the entire code snippet is not mine. One thing that you should think of is making web safe names and limiting the file upload type to jpg. If the moderators are ok with it I *could* post the script I use with modification to fit your purpose. The only problem is I cannot remember all the places I got the parts for it from to properly credit everyone.

it's there
(xampp/htdocs/test/test001/avatars/)

That may be your problem then. $location is set to "avatars/$name", but your avatars folder is two levels down from htdocs, where the server is looking. Unless you're using something like mod_rewrite, $location should be "test/test001/avatars/$name". Or better yet:

$location = IMAGE_PATH . "avatars/$name";

Where IMAGE_PATH is a manifest constant that holds "test/test001/".

That may be your problem then. $location is set to "avatars/$name", but your avatars folder is two levels down from htdocs, where the server is looking. Unless you're using something like mod_rewrite, $location should be "test/test001/avatars/$name". Or better yet:

$location = IMAGE_PATH . "avatars/$name";

Where IMAGE_PATH is a manifest constant that holds "test/test001/".

When I coded this:

$location = IMAGE_PATH . "avatars/$name";

Warning: move_uploaded_file(test/test001/avatars/CIMG7526.JPG) [function.move-uploaded-file]: failed to open stream: No such file or directory in
D:\xampp\htdocs\test\test001\upload.php on line 21

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'D:\xampp\tmp\phpE0.tmp' to 'test/test001/avatars/CIMG7526.JPG' in D:\xampp\htdocs\test\test001\upload.php on line 21

Your avatar has been uploaded! HOME

===
when i searched the 2nd warning, it says that i need to use ftp, and directory should be chmod 777.

im getting more confused, do i need to do those ftp and chmod stuff? i dled filezilla (client and server) already.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.