Image Upload in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
z0
Forum Newbie
Posts: 15
Joined: Wed Sep 05, 2007 7:17 am

Image Upload in PHP

Post by z0 »

scottayy | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am trying to upload a image from my computer to the "Image" Folder created in C:\wamp\www\.(My Document root is set to C:/wamp/www/test) & when I run this program it should display the image in the same browser window.But the following code is not working.

Code: Select all

<html>
<head><title>Upload Files</title>
</head>
<body>
</br>
<center>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="img"/><br>
<input type="submit" value="click to upload" name="sub">
</form>
<? php
if(isset($_POST['sub']))
{
$file=$_FILES["img"]["name"];
$dir="images/";
$tot_dir=$dir.$file;
if(move_uploaded_file($_FILES["img"]["tmp_name"],$tot_dir))
{
?>
<img src="images/<?php echo $file; ?> "border="0" />
<?php
}
?>
</center>
</body>
</html>
Any suggestion what is wrong with the above code?
Any help would be greatly appreciated.
Thanks


scottayy | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Last edited by z0 on Thu Sep 20, 2007 6:06 am, edited 3 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The extra space after the file echo? Also, note that some browsers send full path information for the name, therefore you need to use basename().
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

feyd wrote:The extra space after the file echo? Also, note that some browsers send full path information for the name, therefore you need to use basename().
Good eye on that space. I looked over the code and didn't see anything.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post by SidewinderX »

Not exactly sure whats not working, but on line 19 - this doesn't look kosher.

Code: Select all

<? php
z0
Forum Newbie
Posts: 15
Joined: Wed Sep 05, 2007 7:17 am

re:upload img

Post by z0 »

I am getting the following error when i click the "Upload" button ->
"Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\wamp\tmp\php10.tmp' to 'images/abc.jpg' in C:\wamp\www\test\upload.php on line 17"
And,I am using Firefox Browser.
And, in which line have i given the extra space after "echo"? :roll:
Kindly if you guide me...
Also, is these code line correct? ->
<img src="images/<?php echo $file; ?> "border="0" />
I feel there is somthing wrong :!: :roll:
Last edited by z0 on Thu Sep 20, 2007 5:27 am, edited 1 time in total.
User avatar
kreoton
Forum Commoner
Posts: 42
Joined: Fri Mar 03, 2006 7:27 pm

Post by kreoton »

use fullpath for $tot_dir in example:

Code: Select all

$dir=$_SERVER['DOCUMENT_ROOT']."/images/";
$tot_dir=$dir.$file;
z0
Forum Newbie
Posts: 15
Joined: Wed Sep 05, 2007 7:17 am

Post by z0 »

Sir,
But I am still getting these error->
Warning: move_uploaded_file(/images/Blue hills.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in C:\wamp\www\test\upload.php on line 18

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\wamp\tmp\php33.tmp' to '/images/Blue hills.jpg' in C:\wamp\www\test\upload.php on line 18
:oops:
I had supplied my DOCUMENT_ROOT as "C:/wamp/www/test" in -where "test" is a folder I have created,where all my php files are stored under www folder. My "images" folder where I want to upload the images is located in -"C:\wamp\www\images".
i.e.-"C:\wamp\www\images"->is my image upload folder

- "C:/wamp/www/test"->is my php folder
Kindly help me as I am a total novice.
Last edited by z0 on Thu Sep 20, 2007 5:58 am, edited 1 time in total.
mad_phpq
Forum Commoner
Posts: 85
Joined: Fri Apr 27, 2007 5:53 am

Post by mad_phpq »

looks like you need to give write permissions on your folder. Are you using linux or windows?
z0
Forum Newbie
Posts: 15
Joined: Wed Sep 05, 2007 7:17 am

re:img upload

Post by z0 »

No..I am using Windows XP Pro sp2.
I have ->
PHP Version: 5.2.4
Display Errors: On
Error Level: Not E_ALL
Register Globals: Off

No,I haven't given any write permissions to any folder!! :(

But after giving write permissions to the image folder as you suggested, I am still getting these errors->
Warning: move_uploaded_file(/images/Winter.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in C:\wamp\www\test\upload.php on line 18

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\wamp\tmp\php40.tmp' to '/images/Winter.jpg' in C:\wamp\www\test\upload.php on line 18
Last edited by z0 on Thu Sep 20, 2007 11:57 am, edited 1 time in total.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

Paste your latest source after you fix the following:

1. Never, never do file uploads with names provided by the user. This is a severe security hole.
[s]2. If you want those images to be displayed, they should be below the http-visible root, i.e. C:/wamp/www/test/uploads
Alternatively, keep them in c:/wamp/images/, and serve them through a script (header() + readfile(), search the forum for details)[/s]
Aah, you're maybe not doing this.
3. Something is wrong with your code, it shouldn't be /images/Winter.jpg, but c:\wamp\www\images
Post Reply