Upload a file using PHP
Below is one way to upload files, there are many other ways.
As @nordenheim said, $HTTP_POST_FILES
has been deprecated since PHP 4.1.0, thus not advisable to use so.
PHP Code (upload.php)
<?php
$target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if (isset($_POST["submit"])) {
if ($target_file == "upload/") {
$msg = "cannot be empty";
$uploadOk = 0;
} // Check if file already exists
else if (file_exists($target_file)) {
$msg = "Sorry, file already exists.";
$uploadOk = 0;
} // Check file size
else if ($_FILES["fileToUpload"]["size"] > 5000000) {
$msg = "Sorry, your file is too large.";
$uploadOk = 0;
} // Check if $uploadOk is set to 0 by an error
else if ($uploadOk == 0) {
$msg = "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
$msg = "The file " . basename($_FILES["fileToUpload"]["name"]) . " has been uploaded.";
}
}
}
?>
HTML Code to start function
<form action="upload.php" method="post" id="myForm" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<button name="submit" class="btn btn-primary" type="submit" value="submit">Upload File</button>
</form>
Hope this helps.
upload a file using php script without leaving that page
There are two choices really here. You could use the following code as the action for the form and then have the php in the same file.
<?php echo $_SERVER['PHP_SELF']; ?>
Or
header("Location: upload.html?new_name=value_of_new_name");
In the upload.php where its successful, then in upload.html you can use $_GET['new_name']
to retrieve the value of $new_name
Unable to upload file using PHP
You should prepend the root directory (i.e. the directory under which the specified directory exists / will be created) to the directory name entered.
Also make sure there is a trailing slash using either client side or server side validation since users may forget to enter it.
Best and Secure way to upload file using php
This question is already asked here.
but you can use my own code for same it is simple and secure.
function hc_upload($f,$username='',$verify_type=1,$size=2048)
{
$f=$_FILES[$f];
$file_name=strtolower($f['name']);
$file_type=strtolower($f['type']);
$file_size=strtolower($f['size']);
$file_extenstion =end(explode('.',$file_name));
$file_extenstion2=strtolower(pathinfo(basename($file_name),PATHINFO_EXTENSION));
if($file_extenstion2!=$file_extenstion){
$err["error"]=true;
$err["message"]="Invalid file extension.";
return $err;
}
if($file_size > $size*1000){
$err["error"]=true;
$err["message"]="File is too large.";
return $err;
}
$ext_verify=0;
if(gettype($verify_type)!='array')
{
$verify_type=(string)$verify_type;
if((strpos($verify_type,"1") > -1 || $verify_type=="*") && $ext_verify==0)
{
$mimes['ext']=array("jpg","jpeg","gif","png");
$mimes['mime']=array("image/jpg","image/jpeg","image/gif","image/png");
if(in_array($file_extenstion,$mimes['ext']) && in_array($file_type,$mimes['mime'])){$ext_verify=1;}
}
if((strpos($verify_type,"2") > -1 || $verify_type=="*") && $ext_verify==0)
{
$mimes['ext']=array("doc","docx","pdf","xls","xlsx","ppt","pptx");
if(in_array($file_extenstion,$mimes['ext'])){$ext_verify=1;}
}
if((strpos($verify_type,"3") > -1 || $verify_type=="*") && $ext_verify==0)
{
$mimes['ext']=array("mp3","wav","weba","3gp","mp4","mov","mpeg","avi");
$mimes['mime']=array("audio/mpeg","audio/wav","audio/webm","audio/3gpp","video/3gpp","video/mp4","video/quicktime","video/mpeg","video/x-msvideo");
if(in_array($file_extenstion,$mimes['ext']) && in_array($file_type,$mimes['mime'])){$ext_verify=1;}
}
}
else
{
if(array_key_exists("mime",$verify_type) && array_key_exists("ext",$verify_type)){
if(in_array($file_extenstion,$verify_type['ext']) && in_array($file_type,$verify_type['mime'])){$ext_verify=1;}
}
elseif(array_key_exists("ext",$verify_type)){
if(in_array($file_extenstion,$verify_type['ext'])){$ext_verify=1;}
}
elseif(array_key_exists("mime",$verify_type)){
if(in_array($file_type,$verify_type['mime'])){$ext_verify=1;}
}
else{
if(in_array($file_extenstion,$verify_type)){$ext_verify=1;}
}
}
if($ext_verify==0){
$err["error"]=true;
$err["message"]="Seems your file is not valid";
return $err;
}
$upload_dir='upload/'.$username.'/';
if(!is_dir($upload_dir)){
if(!mkdir($upload_dir,0777,true)){
$err["error"]=true;
$err["message"]="Unknown error, kindly contact admin";
return $err;
}
}
$upload_file=$upload_dir.sha1_file($f['tmp_name']);
if(!file_exists($upload_file)){
if(!move_uploaded_file($f['tmp_name'], $upload_file)){
$err["error"]=true;
$err["message"]="Unknown error, kindly contact admin";
return $err;
}
}
$err["error"]=true;
$err["message"]="SUCCESS";
$err["dir"]=$upload_file;
return $err;
}
And HTML Sample Code is
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
So simply call this function in php
hc_upload('fileToUpload','',123)
you can create folder for each username by giving value two second parameter
and third parameter two check for file is image or document or audio/video media and also can pass array of extensions for manually check
Related Topics
Preserve Line Breaks from Textarea
Special Characters in Property Name of Object
How to Perform Static Code Analysis in PHP
What's the Difference Between :: (Double Colon) and -≫ (Arrow) in PHP
Download Multiple Files as a Zip-File Using PHP
Get the Last Inserted Id Using Laravel Eloquent
Simple Jquery, PHP and Jsonp Example
Insert Array into MySQL Database With PHP
Max Size of Url Parameters in _Get
Redraw Google Chart Based on User Input Via Ajax Request
How to Minify PHP Page HTML Output
Having Issue With Matching Rows in the Database Using Pdo
How to Put Composite Keys in Models in Laravel 5
Pdo Binding Values For MySQL in Statement