Hi all,

I am making one application in which i need some PHP code . My means i want to upload an image that can be in .jpeg, .jpg or .gif format. That i want to upload from my system and after uploading that image and i want to show that image on that page from which i am uploading that image.

All of this i want to do using PHP code . I know just i should use move_upload function . But i do not know in correct format how i should use this function.
So Please help me to solve this problem. I will be thankful of that person who will he help me on this topic because i am doing such type of work first time.
So please help me.
Thanks,
Gagan

build upload.php and success.php..

upload.php contains:

<?php

/// For the following details,
/// please contact your server vendor

$hostname='localhost'; //// specify host, i.e. 'localhost'
$user='***********'; //// specify username
$pass='***********'; //// specify password
$dbase='uploads'; //// specify database name
$connection = mysql_connect("$hostname" , "$user" , "$pass")
or die ("Can't connect to MySQL");
$db = mysql_select_db($dbase , $connection) or die ("Can't select database.");

if(!empty($_POST["submit"]))
{

            if(empty($_FILES["f_Filename"]["tmp_name"]))
            $error[]="File is a required field.";  // Check if File is empty.
            else
            {
            
			if(strstr($_FILES["f_Filename"]["type"] , "image"))
			$error[]="File needed to be an image.";
			
			}
            
        if(empty($error))
        {

         if(!empty($_FILES["f_Filename"]["tmp_name"]))
         {
         move_uploaded_file($_FILES["f_Filename"]["tmp_name"] , "images/".basename($_FILES["f_Filename"]["name"])) OR die("Could not upload");
         $f_Filename=mysql_real_escape_string("images/" .$_FILES["f_Filename"]["name"]);
         }

        $sql="INSERT INTO images (`Filename`) VALUES ('$f_Filename') ";

        mysql_query($sql) or die(mysql_error());
        header("Location: success.php");

        }
        else
        print "Please check these errors:<br />" . implode("<br />" , $error);

        /*  table create code!
        CREATE TABLE `images` (

            `Filename` VARCHAR( 255 ) CHARACTER SET utf8 NOT NULL
          ,`imageid` int(11) NOT NULL auto_increment,
  PRIMARY KEY  (`imageid`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;


        */
} //if submit has pressed!

echo "<form name=\"aaaa\" method=\"post\"  enctype=\"multipart/form-data\" action=\"upload.php\">";
echo 'File<input name="f_Filename" type="file"><br />'."\n";;
echo '<input name="submit" type="submit" value="submit">';
print '</form>';

?>

success.php will list the images from the images table.

hi ,

thanks for reply,

i have done as you provide code me.

But i am getting this error

Please check these errors:
File needed to be an image.

Actually i am uploading already an jpeg image file. But why i am getting such type of error.
i do not know . Please help me to solve this.
Thanks,

change the code according to this, i forget to negate this example:

if(strstr($_FILES["f_Filename"]["type"] , "image")==false)
			$error[]="File needed to be an image.";

change the code according to this, i forget to negate this example:

if(strstr($_FILES["f_Filename"]["type"] , "image")==false)
			$error[]="File needed to be an image.";

hi, thanks for reply,

But now i am getting error
could not upload

How i can solve this now ?? sorry for again asking actually i am doing such type of work first time.
pls help me .

you need to create 'images' folder, where the images will be uploaded. If you're testing on a webserver, lets set its permission at least to 744

you need to create 'images' folder, where the images will be uploaded. If you're testing on a webserver, lets set its permission at least to 744

thanks for reply,

yes, i have already created one folder in webserver naming " images" and i see its permission also . This have 755 already in my Ftp.

so i am not understanding why i am getting this error after submitting image:-

Could not upload

Need Help

http://phpcode.hu/upl/upload.php

this is the demo code, wthout the SQL insert.

images folder (whitespace should not be placed on the begining of the folder name.)

http://phpcode.hu/upl/upload.php

this is the demo code, wthout the SQL insert.

images folder (whitespace should not be placed on the begining of the folder name.)

thanks for so much co-operating , But i am not understanding where is wrong my code.

ok i am sending my code which i am using in uploadphoto.php file

i think after see my code you can help me more :


this is code of uploadphoto.php file:-

if(!empty($_POST["submit"]))
{

            if(empty($_FILES["file1"]["tmp_name"]))
            $error[]="File is a required field.";  // Check if File is empty.
            else
            {
            
			if(strstr($_FILES["file1"]["type"] , "image")==false)
			$error[]="File needed to be an image.";

			
			}
            
        if(empty($error))
        {

         if(!empty($_FILES["file1"]["tmp_name"]))
         {
		 $name=$_FILES["file1"]["name"];
		 $file=$_FILES["file1"]["tmp_name"];
         move_uploaded_file($file ,"images/$name") OR die("Could not upload");
         $f_Filename=mysql_real_escape_string("images/" .$_FILES["file1"]["name"]);
         }

        $sql="INSERT INTO images(Filename) VALUES ('$f_Filename') ";

        mysql_query($sql) or die(mysql_error());
        header("Location: showphoto.php");

        }
        else
        print "Please check these errors:<br />" . implode("<br />" , $error);

        /*  table create code!
        CREATE TABLE `images` (

            `Filename` VARCHAR( 255 ) CHARACTER SET utf8 NOT NULL
          ,`imageid` int(11) NOT NULL auto_increment,
  PRIMARY KEY  (`imageid`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;


        */
} //if submit has pressed!




?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Employee Gallery Application</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="css/stylesheet.css" rel="stylesheet" type="text/css">
<link href="css/stylesheet2.css" rel="stylesheet" type="text/css">
<link href="css/stylesheet1.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
.style7 {
	font-family: "Courier New", Courier, monospace;
	font-size: 18px;
	color: #FF0000;
}
-->
</style>
</head>

<body>
<table width="100%"  border="0" cellspacing="0" cellpadding="0">
  <tr>
  <td height="30">&nbsp;</td>
  </tr>
  <tr>
    <td  class="header-bg" height="28"><? include("header.php");?></td>
  </tr>
  <tr>
    <td height="25" valign="bottom" class="text"></td>
  </tr>
</table>
  <table width="780" height="15"  border="0" align="center" valign="top" cellpadding="0" cellspacing="0">
    <tr>
      <td width="700" height="29" background="image/bodylinkbgcolor.jpg"><div align="center"></div></td></tr>
    <tr>
      <td class="bodybg"><table width="780" border="0" cellspacing="0" cellpadding="0">
        <tr align="center">
          <td width="342" height="0" class="bodytext1"><span class="username">Employee  Name : </span>           <strong><? echo $_SESSION['name'];?></strong></td>
          <td width="411" class="bodytext1"><span class="username">Employee ID : </span>         <strong><? echo $_SESSION['empid'];?></strong></td>
        </tr>
		
		
        <tr>
          <td  colspan="2" align="center" class="forget" style="color:#FF0000;" >&nbsp;</td>
        </tr>
		<tr>
       		    <td colspan="2">
       		      
       		      <marquee>
   		          <span class="style7">Hi <strong><? echo $_SESSION['name'];?></strong>, Welcome You in Gallery Section </span>
   		          </marquee>       		      </td>
		</tr>
		<tr>
		<td colspan="2">&nbsp;</td>
		</tr>
       		  
       		  <tr>
       		    <td colspan="2" >
       		      <table width="98%"  border="0" align="center" cellpadding="0" cellspacing="0">
				   
       		      <tr>
       		        <td width="1%" bgcolor="#FFFFFF" scope="row"></td>
                    <td width="98%" bgcolor="#FFFFFF" scope="row">
					
					<form action="" name="form1" method="post" enctype="multipart/form-data">
					<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center" valign="top" width="50%"><p>&nbsp;</p><strong class="client_login">Upload Photo</strong></td>
    <td align="left" valign="top" width="50%"><p>&nbsp;</p><input type="file" name="file1"><p>&nbsp;</p></td>
  </tr>
  
  <tr>
  <td colspan="2" align="center"><input name="submit" type="submit" value="Upload Photo"><p>&nbsp;</p> 
  </td>
  </tr>
  
</table>			</form>		</td>
                    <td width="1%" bgcolor="#FFFFFF" scope="row"></td>
              </tr>   </table></td>
	      </tr>
       		  <tr>
       		    <td colspan="2" >&nbsp;</td>
	    </tr>
       		  
        </table></td>
    </tr>
  </table>

 
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td height="30">&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
  </table>
</body>
</html>

I think pls see my code which i am using. I am getting this error right now :
Could not Upload

Need Help

change: $name=$_FILES["file1"]["name"]; to: $name=basename($_FILES["file1"]["name"]); And change this: $f_Filename=mysql_real_escape_string("images/" .$_FILES["file1"]["name"]); into: $f_Filename=mysql_real_escape_string("images/" .basename($_FILES["file1"]["name"]) ); basename was missing from your code.

change: $name=$_FILES["file1"]["name"]; to: $name=basename($_FILES["file1"]["name"]); And change this: $f_Filename=mysql_real_escape_string("images/" .$_FILES["file1"]["name"]); into: $f_Filename=mysql_real_escape_string("images/" .basename($_FILES["file1"]["name"]) ); basename was missing from your code.

hi sir,

as you asked me i put basename in front of that line which you told me but still i am getting such error.
could not upload
on same page.
Help ?

http://phpcode.hu/upl/source.txt

i've added the mentioned basename

hi sir,
after doing this as same as code you provide me still i am getting same error

could not upload

i've changed the code not to upload the file name into a database.

http://phpcode.hu/upl/code.rar

Hi sir,

Thanks for you are doing so much help to me . But still i am facing same error.
As i upload image file from this form and click on submit button after that in this file which is upload.php just printing one line top left cornor of the browser:

Could not upload

i am going to confuse and you will also going on ??? where the problem is occurring????
this is going to very puzzled????

change the permittion to 777 ... for the images folder.

i think this is permittion problem.

change the permittion to 777 ... for the images folder.

i think this is permittion problem.

hi,
i have changed the permission of images folder in my FTP 775 as you asked me still am getting same error :-
Could not upload

r read
w write
e execute

775 means
owner: r w e
group: r w e
World(Other) r e

the world don't have write permit for that folder.

r read
w write
e execute

775 means
owner: r w e
group: r w e
World(Other) r e

the world don't have write permit for that folder.

yes i know that sir,

so i have given it 775 permission in this folder also

7 is for owner
7 is for group
5 is for public

still m facing
this error
could not upload

pls help me to solve this
thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.