Page 1 of 1

Image Upload in PHP

Posted: Wed Sep 19, 2007 10:50 pm
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]

Posted: Wed Sep 19, 2007 11:35 pm
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().

Posted: Thu Sep 20, 2007 1:31 am
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.

Posted: Thu Sep 20, 2007 2:29 am
by SidewinderX
Not exactly sure whats not working, but on line 19 - this doesn't look kosher.

Code: Select all

<? php

re:upload img

Posted: Thu Sep 20, 2007 2:52 am
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:

Posted: Thu Sep 20, 2007 3:04 am
by kreoton
use fullpath for $tot_dir in example:

Code: Select all

$dir=$_SERVER['DOCUMENT_ROOT']."/images/";
$tot_dir=$dir.$file;

Posted: Thu Sep 20, 2007 5:50 am
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.

Posted: Thu Sep 20, 2007 5:54 am
by mad_phpq
looks like you need to give write permissions on your folder. Are you using linux or windows?

re:img upload

Posted: Thu Sep 20, 2007 6:00 am
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

Posted: Thu Sep 20, 2007 9:17 am
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