PHP File Upload Problems

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
birendrashrestha
Forum Newbie
Posts: 7
Joined: Thu Nov 26, 2009 11:05 pm

PHP File Upload Problems

Post by birendrashrestha »

Hi I am using the following code to upload a file to my host server but it's not working I Keep getting the following error can anyone help?

<form action="images/ImgaeUpload.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />





<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

if (file_exists("images/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"images/" . $_FILES["file"]["name"]);
echo "Stored in: " . "images/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>




The Error that I am getting


Warning: move_uploaded_file(images/naruto.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/a4110155/public_html/images/ImgaeUpload.php on line 25

Free Web Hosting

PHP Error Message

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpbRTmKv' to 'images/naruto.jpg' in /home/a4110155/public_html/images/ImgaeUpload.php on line 25
manojsemwal1
Forum Contributor
Posts: 217
Joined: Mon Jun 29, 2009 4:13 am
Location: India

Re: PHP File Upload Problems

Post by manojsemwal1 »

just use the following script

$filename=$_FILES["file"]["name"];
$random_digit=rand(0000,9999);
$newname = 'upload/'.$random_digit.$filename;
$path = '../../ClientEntry/upload/'.$random_digit.$filename;
//echo $path;

$newpath= move_uploaded_file($_FILES["file"]["tmp_name"],$path);
birendrashrestha
Forum Newbie
Posts: 7
Joined: Thu Nov 26, 2009 11:05 pm

Re: PHP File Upload Problems

Post by birendrashrestha »

I don't know much about php i tried out the script you posted but i am still getting the same error

here is what i m doing can you look at it and tell me if i am doing something wrong.


<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

if (file_exists("images/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
$filename=$_FILES["file"]["name"];
$random_digit=rand(0000,9999);
$newname = 'images/'.$random_digit.$filename;
$path = '../../ClientEntry/images/'.$random_digit.$filename;
//echo $path;
$newpath= move_uploaded_file($_FILES["file"]["tmp_name"],$path);
}
}
}
else
{
echo "Invalid file";
}
?>


The error that I am getting

Warning: move_uploaded_file(../../ClientEntry/images/721naruto.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/a4110155/public_html/ImgaeUpload.php on line 29

Free Web Hosting

PHP Error Message

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpXs68Jv' to '../../ClientEntry/images/721naruto.jpg' in /home/a4110155/public_html/ImgaeUpload.php on line 29
manojsemwal1
Forum Contributor
Posts: 217
Joined: Mon Jun 29, 2009 4:13 am
Location: India

Re: PHP File Upload Problems

Post by manojsemwal1 »

ya i had checked your scripting in my server u do one thing just remove out the ImgaeUpload.php file from the images folder and keep it out from folder. and try it. defenetly it will work....
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: PHP File Upload Problems

Post by daedalus__ »

what?

did that work?

check the permissions on the directory.
manojsemwal1
Forum Contributor
Posts: 217
Joined: Mon Jun 29, 2009 4:13 am
Location: India

Re: PHP File Upload Problems

Post by manojsemwal1 »

yes

give the permission rw
ok
birendrashrestha
Forum Newbie
Posts: 7
Joined: Thu Nov 26, 2009 11:05 pm

Re: PHP File Upload Problems

Post by birendrashrestha »

Thanks Guys you Guys are awesome! I feel so stupid hahaha i should have checked the permission. That was my problem the permission. Thanks a ton guys! :)
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: PHP File Upload Problems

Post by daedalus__ »

youre welcome!
Image
birendrashrestha
Forum Newbie
Posts: 7
Joined: Thu Nov 26, 2009 11:05 pm

Re: PHP File Upload Problems

Post by birendrashrestha »

I also had one more question sorry i forgot about it earlier.

I was trying to make it so that i can give the ability to browse the server where my images are uploaded through a form so users can browse for it and choose a image and the path to that image will be stored in a field in the form more like a what type="File" does but instead of searching the computer i wanted it to be able to browse the server so they can select the images. Can any of you help me with this? Is there a script in php for doing thin this?

Thanks A lot guys for your help already!
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: PHP File Upload Problems

Post by daedalus__ »

you lost me at "choose a image and the path to that image will be stored in a field in the form more like a what type="File""

as far as i know, you won't be able to use forms to browse anything on the server. they simply weren't designed that way. but you could find or write some code that displays the contents of the image directory then uses forms to interpret how the user would like to manipulate the data.

what are you trying to accomplish with your project? (try to be clear and concise)
birendrashrestha
Forum Newbie
Posts: 7
Joined: Thu Nov 26, 2009 11:05 pm

Re: PHP File Upload Problems

Post by birendrashrestha »

I am sorry i am being confusing.

I am trying to build a CMS for a simple website that i am trying to build and i want the users to be able to insert pictures on a certain page which will be already uploaded on the server. How can I do this?
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: PHP File Upload Problems

Post by daedalus__ »

you mean like an icon or a smiley face like on the right hand side of the reply page on this forum?

you could start by creating a function that compiles a list of images from a directory.

after you have your list you could create a form that has radio button followed by the image, with the radio button referencing the path to the image.

then the user selects the image they want from the list in your form. they click submit and the path of the image is sent back to the server ready to use.

it's more involved than that, but i broke it down in to simple steps. if you need help, we're here. :D
birendrashrestha
Forum Newbie
Posts: 7
Joined: Thu Nov 26, 2009 11:05 pm

Re: PHP File Upload Problems

Post by birendrashrestha »

hi i am trying to dis play 4 results in one row and if there is more dis play it in another row

here is the code its not working can you help.

<?php
while($row = mysql_fetch_array($query))
{$count = 1;
?>
<tr>
<?php
while($count < 5)
{
?>
<td>

<a href="photos.php?ID=<?php echo"".$row['ID'] ?>"><img src="<?php echo"".$row['ImageLocation']?>" alt="NO IMAGE" height="100" width="100"></a></td>
<?php
$count = 6;
}
?>

</tr>
<?php

}
?>
birendrashrestha
Forum Newbie
Posts: 7
Joined: Thu Nov 26, 2009 11:05 pm

Re: PHP File Upload Problems

Post by birendrashrestha »

never mind i think i figured it out! thanks any way
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: PHP File Upload Problems

Post by daedalus__ »

reset count to 1 after it reaches four

or say

Code: Select all

 
if ($count % != 0)
{
   //blah
}
 
Post Reply