Picture Upload?

Hi,
I’m a new member and hope to receive some great feedback here. Thanks for the opportunity to post.
So here is my issue. I am building a php forum site where users login and post a picture, title, description etc. Problem is I can’t figure out for the life of me why the picture is not uploading. I was getting the error Notice: Undefined index: picture but made changes that I thought fixed that and no error now, only the picture is still not appearing. Below is my code for the newposting.inc.php and addposting.inc.php files. Any suggestions would be greatly appreciated.

newposting.inc.php file code

addposting.inc.php file code

i have amended your script as you forgot? to add the function to actually move the file…

[php]
if ($result)

/*
I added this bit to move the file to its new location.
REMEMBER TO SET THE PATH!
*/
$path = “path/to/put/file/”;
$path = $path . basename( $_FILES[‘picture’][‘name’]);
if(move_uploaded_file($_FILES[‘picture’][‘tmp_name’], $path))
{
echo "The file “. basename( $_FILES[‘picture’][‘name’]).
" has been uploaded”;
}
else
{
echo “There was an error uploading the file, please try again!”;
}

// End of added sectoin

echo “

Posted


\n”;

else

echo “

Sorry, there was a problem posting your post

\n”;

}

[/php]

This is where it belongs in the script, :wink:

Ok so I get this error now…please help. My the code is below that, does it look correct?

Warning: move_uploaded_file(path/to/put/file/facebook_profile_logo_round.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /Applications/MAMP/htdocs/rockmyfashion/addposting.inc.php on line 41

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move ‘/Applications/MAMP/tmp/php/phpiE8EN4’ to ‘path/to/put/file/facebook_profile_logo_round.jpg’ in /Applications/MAMP/htdocs/rockmyfashion/addposting.inc.php on line 41
There was an error uploading the file, please try again!
Posted
Sorry, there was a problem posting your post
[php]$title = $_POST[‘title’];

$poster = $_POST[‘poster’];

$shortdesc = $_POST[‘shortdesc’];

$picture = file_get_contents($_FILES[‘picture’][‘tmp_name’]);

$picture = mysql_real_escape_string($picture);

if (trim($poster == ‘’))

{

echo “

Sorry, each posting must have a poster

\n”;

}else

{

$con = mysql_connect(“localhost”, “test”, “test”) or die(‘Could not connect to server’);

mysql_select_db(“fashion1”, $con) or die(‘Could not connect to database’);

$query = "INSERT INTO postings (title, shortdesc, poster, picture) " .

" VALUES (’$title’, ‘$shortdesc’, ‘$poster’, ‘$picture’)";

$result = mysql_query($query) or die(mysql_error());

if ($result)

$path = “path/to/put/file/”;
$path = $path . basename( $_FILES[‘picture’][‘name’]);
if(move_uploaded_file($_FILES[‘picture’][‘tmp_name’], $path))
{
echo "The file “. basename( $_FILES[‘picture’][‘name’]).
" has been uploaded”;
}
else
{
echo “There was an error uploading the file, please try again!”;
}

echo “

Posted


\n”;

else

echo “

Sorry, there was a problem posting your post

\n”;

}
[/php]

quickly scanning through your post i can see a few errors…
[php]

<?php $title = $_POST['title']; $poster = $_POST['poster']; $shortdesc = $_POST['shortdesc']; $picture = file_get_contents($_FILES['picture']['tmp_name']); $picture = mysql_real_escape_string($picture); if (trim($poster == '')) { echo "

Sorry, each posting must have a poster

\n"; } else { $con = mysql_connect("localhost", "test", "test") or die('Could not connect to server'); mysql_select_db("fashion1", $con) or die('Could not connect to database'); $query = "INSERT INTO postings (title, shortdesc, poster, picture) " . " VALUES ('$title', '$shortdesc', '$poster', '$picture')"; $result = mysql_query($query) or die(mysql_error()); if ($result) { // <------------------------------------- Opening bracket missing here! $path = "path/to/put/file/"; $path = $path . basename( $_FILES['picture']['name']); if(move_uploaded_file($_FILES['picture']['tmp_name'], $path)) { echo "The file ". basename( $_FILES['picture']['name']) . " has been uploaded"; } else { echo "There was an error uploading the file, please try again!"; } echo "

Posted


\n"; } // <------------------------------------- Closing bracket missing here! else { // <------------------------------------- Opening bracket missing here! echo "

Sorry, there was a problem posting your post

\n"; } // <------------------------------------- Closing bracket missing here! } ?>

[/php]

I haven’t tried this code, just pointing out the errors. let me know if that fixes it. :smiley:

First of all I just wanted to take the time to thank you for a helping a complete stranger :smiley:
Now back to it, I made those changes and now I get this error/warning:

Warning: move_uploaded_file(path/to/put/file/facebook_profile_logo_round.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /Applications/MAMP/htdocs/rockmyfashion/addposting.inc.php on line 42

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move ‘/Applications/MAMP/tmp/php/phpAWN8sU’ to ‘path/to/put/file/facebook_profile_logo_round.jpg’ in /Applications/MAMP/htdocs/rockmyfashion/addposting.inc.php on line 42
There was an error uploading the file, please try again!

Line 42 is
[php]if(move_uploaded_file($_FILES[‘picture’][‘tmp_name’], $path))[/php]

see this bit:
[php]$path = “path/to/put/file/”;[/php]

is this where your trying to put the file??

if it is where your trying to put the file, that is not the correct path!

this is your path: /Applications/MAMP/htdocs/rockmyfashion/
and from there if you wanted to save the file in a folder called ‘myUploads’
then your path becomes: /Applications/MAMP/htdocs/rockmyfashion/myUploads/

make sense?

Let me know if that helps :smiley:

PS:
In my first post it clearly states in capitol letters:
[php]
/*
I added this bit to move the file to its new location.
REMEMBER TO SET THE PATH!
*/
$path = “path/to/put/file/”;
[/php]

But if users are logging in and posting a picture that wouldn’t be the path I need to set, would it?

i mean you no disrespect but if you can’t understand this simple line of code:
[php]$path = “path/to/put/file/”;[/php] you really shouldn’t be altering any php files just yet…

maybe this can help you: PHP Manual: File Uploads

Thanks for all your help!

i’ll try and explain about the path to you see if we can get you off and running :slight_smile:

from your first post i see this: /Applications/MAMP/htdocs/ :this is the path to your public folder - (where visitors come to see your website) - this will never change unless you move to a different host.

i can also see from your post you have a folder called: rockmyfashion : and inside that folder is the script addposting.inc.php (which i assume is your uploading script.)

Right, so we now know the path to the public folder(htdocs),
we now need somewhere to keep uploaded images all together in the same place.
inside the htdocs folder we create another folder called: myUploads : the path to this folder looks like this: /Applications/MAMP/htdocs/myUploads/ :

So this:
[php]$path = “path/to/put/file/”;[/php]

becomes this:
[php]$path = “/Applications/MAMP/htdocs/myUploads/”;[/php]

Does that make sense?

Good luck, and keep persevering, you’ll look back at this post one day and giggle :wink:

Ps: there is a function that helps with the path (especially if you don’t know it)
[php]$_SERVER[‘DOCUMENT_ROOT’]; [/php] this will always point at the public folder ‘htdocs’ so from there we simply add the rest of the path like this: [php]$_SERVER[‘DOCUMENT_ROOT’] . ‘/myUploads/’; [/php]

Thanks for sticking with me, you are probably right that I’m not quite ready but I have a job to do. Actually most everything has been smooth aside from the picture issue. Ok so I made revisions and now I get this.
The file facebook_profile_logo_round.jpg has been uploaded
Now the posting shows but still no picture.
Maybe something is wrong in my main.inc.php file that shows all the postings?
[php]$con = mysql_connect(“localhost”, “test”, “test”) or die(‘Sorry, could not connect to database server’);

mysql_select_db(“fashion1”, $con) or die(‘Sorry, could not connect to database’);

$query = “SELECT pictureid,title,poster,picture,shortdesc from postings order by pictureid desc limit 0,5”;
$result = mysql_query($query) or die('Sorry, could not get postings at this time ');

if (mysql_num_rows($result) == 0)
{
echo “

Sorry, there are no postings posted at this time, please try back later.

”;
} else
{
while($row=mysql_fetch_array($result, MYSQL_ASSOC))
{
$pictureid = $row[‘pictureid’];
$title = $row[‘title’];
$poster = $row[‘poster’];
$picture = $row[‘picture’];
$shortdesc = $row[‘shortdesc’];
   echo "<a href=\"index.php?content=showposting&id=$pictureid\"><h2>$title</h2></a> submitted by&#8211;$poster<br>\n";
   echo"$shortdesc<br><br>\n";

}
}[/php]

if you have managed to upload the picture, that’s good news, it means were getting there :smiley:

Now, to display the picture you would use the img tag like any other image:
but we add the row from the database in between the source value like so:
[php][/php]
this is looking in the folder ‘myUploads’ for whatever the value of $row[‘picture’] may be.

If you check the source code you will see something like:
[php][/php]

Let me know how you get on.
:wink:

I’ll try that, thank you!

your welcome :wink:

I know this is not recommended until working correctly but the site is live. I just needed to test it live to really see what was happening. Everything is good, obviously aside from the picture upload.
Sorry, I’m still not understanding the path, don’t I want the path to be in the database ‘picture’ ???
here’s the errors/warnings that I get when I login and post:

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2) in/home/content/17/7435817/html/addposting.inc.php on line 11

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in/home/content/17/7435817/html/addposting.inc.php on line 11

Warning: move_uploaded_file(/Applications/MAMP/htdocs/myUploads/facebook_profile_logo_round.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in/home/content/17/7435817/html/addposting.inc.php on line 42

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move ‘/tmp/php192WHA’ to ‘/Applications/MAMP/htdocs/myUploads/facebook_profile_logo_round.jpg’ in/home/content/17/7435817/html/addposting.inc.php on line 42
There was an error uploading the file, please try again!
Posted

And in regards to your last response to display the picture does this look right.
Code before:
[php]echo “<a href=“index.php?content=showposting&id=$pictureid”>

$title

submitted by–$poster
\n”;
echo"$shortdesc

\n";[/php]

Code after:
[php]echo “<a href=“index.php?content=showposting&id=$pictureid”>

$title

submitted by–$poster
\n”;
echo"$shortdesc

\n";
[/php]

Thanks again for all your help!

the code looks ok, however the top two error warnings you posted are because the script can NOT connect to the database…

If you make a connection, that should be fixed.

the bottom two are uploading errors,
this line:
warning: move_uploaded_file(/Applications/MAMP/htdocs/myUploads/facebook_profile_logo_round.jpg)

is where your trying to move the uploaded picture…

and this line:
[function.move-uploaded-file]: failed to open stream: No such file or directory in/home/content/17/7435817/html/addposting.inc.php on line 42

is telling you there is no directory (folder) to put the uploaded picture (there is no folder called myUploads) so you need to make one, or change this to the folder name your putting them into.
that will fix the upload problems…
:slight_smile:

This is not right
[php]echo “<a href=“index.php?content=showposting&id=$pictureid”>

$title

submitted by&#8211;$poster
\n”;
echo"$shortdesc

\n";
[/php]

It gave me this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘’ at line 1

How do incorporate the img function into this statement appropriately.
Here’s what I have now and no error when posting but also no picture. But it’s definitely not right.
[php]echo “<a href=“index.php?content=showposting&id=$pictureid”>

$title

submitted by–$poster
\n”;
echo"$shortdesc

\n";
echo “$picture\n”;[/php]
Thanks for hangin with me… :smiley:

this bit will cause an error because you have used plain HTML within PHP:

<img src=" echo $row[‘picture’]; " alt="" />

if you want to use this here you need to do either of these:
[php]
?>

<?php // move in and out of HTML/PHP echo "\"\""; // with with quotes - wrapped in braces echo ''; // no quotes. [/php] This will display an image (if $row['picture'] has a value!) that is in the SAME folder as this script. If the image is in a different folder you need to add the name of the folder (the path) before $row['picture'] Example: (i have images stored in a folder called myUploads) [php] echo ''; // without quotes. [/php] or [php] echo "\"\""; // with quotes - needs to be wrapped in braces! [/php] Let me know if that helps ;)

Ok so I got the question mark picture thing to show in the main section. The pictures are still not making it into the database. The error says line11 which is this:
[php]$picture = mysql_real_escape_string($picture);[/php]
There’s got to be something wrong with this statement.
Thoughts?

Sponsor our Newsletter | Privacy Policy | Terms of Service