Uploading images with $_FILES

images php coding issues or problems here.

Moderators: macek, egami, gesf

m99
New php-forum User
New php-forum User
Posts: 4
Joined: Wed Dec 01, 2010 3:06 pm

Uploading images with $_FILES

Postby m99 » Wed Dec 01, 2010 4:06 pm

Hello everyone,
im totally new to PHP coding and this forum and I was wondering if there was anyone out there willing to help me get started uploading images using the $_FILES superglobal.. I ran the following script because I want to give users the ability to upload files(images/photos);

<?php
//this will check if form was submitted
if($_SERVER['REQUEST_METHOD']=='POST') {
//this checks if the file was uploaded without errors
if(isset($_FILES['photo'])
&& is_uploaded_file($_FILES['photo']['temp_name'])
&& $_FILES['photo']['error']==UPLOAD_ERR_OK){
//this will output the contents of files
foreach($_FILES['photo'] as $key => $value) {
echo "$key : $value <br/>";
}
} else {
echo"No File Uploaded!";
}
} else {

?>

<form action="test.php" method= "post"
enctype= "multipart/form-data">
<label for= "photo"> User Photo: </label>
<input type= "file" name="photo"/>
<input type= "submit" value= "Upload a Image here!" />
</form>

<?php } ?>


when I run this script, i am able to browse and selected images to upload.. but when i upload the image the browser does not output the "$keys : $value <br/>", instead the output echo's "no file uploaded!"...

is this because the files that im attempting to upload are too large, or is my script incorrect..?

I apologize if this has been previously addressed.. Im new to all this and I was not able to find anything that was of use to me..Im not looking for an image, just the notification that an image was received and uploaded!!

PLEASE HELP, Thanks!!!

lokanath7
New php-forum User
New php-forum User
Posts: 139
Joined: Mon Nov 15, 2010 12:11 am

Re: Uploading images with $_FILES

Postby lokanath7 » Fri Dec 03, 2010 7:47 am

instead of

if(isset($_FILES['photo'])&& is_uploaded_file($_FILES['photo']['temp_name']) && $_FILES['photo']['error']==UPLOAD_ERR_OK)


u have to use

if(isset($_FILES['photo'])&& is_uploaded_file($_FILES['photo']['tmp_name']) && $_FILES['photo']['error']==UPLOAD_ERR_OK)

the only error was "temp_name" => "tmp_name" but ur code dosent move the required file to any folder. If u want to do that then u hav to use move_uploaded_file("tempname","destination").

m99
New php-forum User
New php-forum User
Posts: 4
Joined: Wed Dec 01, 2010 3:06 pm

Re: Uploading images with $_FILES

Postby m99 » Tue Dec 07, 2010 9:39 pm

thanks lokanath7! this was truly embarrassing!
-Cheers-

lokanath7
New php-forum User
New php-forum User
Posts: 139
Joined: Mon Nov 15, 2010 12:11 am

Re: Uploading images with $_FILES

Postby lokanath7 » Fri Dec 17, 2010 12:11 am

u r welcom

meerakarthik
New php-forum User
New php-forum User
Posts: 5
Joined: Wed Feb 09, 2011 5:33 am

Re: Uploading images with $_FILES

Postby meerakarthik » Wed Feb 09, 2011 5:47 am

this is a code..........................image only not uploading..............but other things fine.........
help me to fix this

<?php include("connect.php"); ?>
<?php

$fname = trim($_POST['fname']);
$lname = trim($_POST['lname']);
$gender = trim($_POST['gender']);
$phone = trim($_POST['txtPhone']);
$email = trim($_POST['mail']);
$location = trim($_POST['location']);
$zipcode = trim($_POST['zip']);
if(isset ($_FILES['image']) && $_FILES['size'] > 0){

$tmpName = $_FILES['image']['tmp_name'];
$fb = fopen($tmpName,'r');
$data = fread($fb,filesize($tmpName));
$data = addslashes($data);
fclose($data);
}
else
{
echo'choose ur image';
}

$query = "INSERT INTO socialdb(fname,lname,gender,dob,phone,email,location,zipcode,image)VALUES('$fname','$lname','$gender','$dob','$phone','$email','$location','$zipcode','$data')";
$results=mysql_query($query,$link);
if($result)
{
echo "Registered Successfully";
}

?>


Return to “PHP coding => Images”

Who is online

Users browsing this forum: No registered users and 2 guests