Jump to content

UPLOAD


pranshu82202

Recommended Posts

 

http://www.w3schools.com/php/php_file_upload.asp

this is my working example.

 

<?

if($_POST['submit'])

{

if($_FILES['file_up']['name'] == "")//File has a value

{ header("Location:".$config_basedir."loadimage.php?e=1");exit; }

elseif($_FILES['file_up']['size'] == 0)// size is legitimate

{ header("Location:".$config_basedir."loadimage.php?e=2");exit;}

 

//This function reads the extension of the file. It is used to determine if the file  is an image by checking the extension.

function getExtension($str)

{

$i = strrpos($str,".");

        if (!$i) { return ""; }       

$l = strlen($str) - $i;       

$ext = substr($str,$i+1,$l);       

return $ext;

}       

$filename = stripslashes($_FILES[file_up]['name']);

//get the extension of the file in a lower case format

$extension = getExtension($filename);

$extension = strtolower($extension);

//if it is not a known extension, we will suppose it is an error and will not  upload the file,

//otherwise we will do more tests

if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))

{

//print error message

header("Location:".$config_basedir."loadimage.php?e=4");exit;

}

 

//Set maximum file size.

if ($_FILES[file_up]>15000)

{

header("Location:".$config_basedir."loadimage.php?e=3");exit;

        }

 

# Renaming the upload file.

 

//This line assigns a random number to a variable. 

$ran_name = rand () ;

 

//This takes the random number you generated and adds a . on the end, so it is ready of the file extension to be appended.

$ran_name = $ran_name.".";

 

//This combines the directory, the random file name, and the extension

$newfile_name =  $ran_name.$extension;

 

// the path with the file name where the file will be stored, upload is the directory name.

$add="c://xampp/htdocs/iTraining/image/$newfile_name";

 

if(move_uploaded_file ($_FILES[file_up][tmp_name], $add))

{

 

$updsql = "UPDATE customers  SET image='".$newfile_name."' WHERE id=".$_SESSION['SESS_USERID'].";";

$updres = mysql_query($updsql);

if(!$updres)

{  die(" Could not query the database ORDERS 3 : <br/>". mysql_error() ); }

header("Location:".$config_basedir."index.php");exit;

 

}

else

{ header("Location:".$config_basedir."loadimage.php?e=5");}

 

}

else

{

switch($_GET['e'])

{

case "1":

$error= "<strong> Fields are empty </strong> <br/>";

echo "<br/><div align='center'> ";

show_error($error);

echo "</div><br/>";

break;

 

case "2":

$error= "<strong> File Size is not Valid </strong> <br/>";

echo "<br/><div align='center'> ";

show_error($error);

echo "</div><br/>";

break;

 

case "3":

$error= "<strong> Your uploaded file size is more than 150KB so please reduce the file size and then upload. <br/>

                        Visit the help page to know how to reduce the file size.<BR> </strong> <br/>";

echo "<br/><div align='center'> ";

show_error($error);

echo "</div><br/>";

break;

 

case "4":

$error= "<strong> Unknown extension! </strong> <br/>";

echo "<br/><div align='center'> ";

show_error($error);

echo "</div><br/>";

break;

 

case "5":

$error= "<strong> Failed to upload file Contact Site admin to fix the problem </strong> <br/>";

echo "<br/><div align='center'> ";

show_error($error);

echo "</div><br/>";

break;

}

?>

<FORM ENCTYPE="multipart/form-data" ACTION="loadimage.php" METHOD=POST>

<h4>Upload the image:

<INPUT NAME="file_up" TYPE="file">

<INPUT TYPE="submit" name="submit" VALUE="Send File"></h4>

</FORM>

<? } ?>

Link to comment
Share on other sites

@pranshu82202, you need to be a bit more specific, PHP upload is a little generic for everyone to understand exactly what you're looking for.

 

@voip03

 

http://www.w3schools.com/php/php_file_upload.asp

this is my working example.

 

<?
if($_POST['submit'])
{
?>

 

See PHP form submission bad practice

 

Also see: Why PHP short open tags are bad

 

This is a bit ugly:

if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 

 

A nice alternative:

 

$allowed = array(
    'jpg',
    'png',
    'gif'
);

if(in_array($extension, $allowed))
{
    // extension is good
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.