开发者

php image upload

开发者 https://www.devze.com 2022-12-26 23:29 出处:网络
How to make a basic PHP uploader? I want my images to save in my htdocs/myfolder/ Here is my code: <form enctype="multipart/form-data" method="post" action="img_uploade

How to make a basic PHP uploader? I want my images to save in my htdocs/myfolder/

Here is my code:

<form enctype="multipart/form-data" method="post" action="img_uploader.php">
    <input type="file" name="fileToUpload" /><br />
    <input type="submit" value="Upload File" />
</form开发者_StackOverflow>


the code below is a simple example touching all the aspects of image or file upload take a look at it and understand it

<?php
/* to do large file uploads open the php.ini file and set "post_max_size = 150M" or the
size you wish and also set "upload_max_filesize = 120M" post_max_size must be greater than upload_max_filesize in oder
to work, also set the "max_input_time" and "max_execution_time" to 300 (5 minutes specified in seconds)
or more if you wish finally set them in your php script as below, also set "memory_limit = 1024M" or what you wish
by default "memory_limit = 128M" Note in Wamp this should be done in C:\wamp64\bin\apache\apache2.4.23\bin\php.ini and 
in C:\wamp64\bin\php\php5.6.25\php.ini or C:\wamp64\bin\php\php7.0.10\php.ini depending on the php version you are using 
the values must all be the same in all scripts */
ini_set('upload_max_filesize', '10M');
ini_set('post_max_size', '50M');
ini_set('max_input_time', 300);
ini_set('max_execution_time', 300);
ini_set('memory_limit','500M');
//set errors array
function output_errors($errors){
    $output = array();
    foreach($errors as $error){
        $output[] = '<li>' . $error . '</li>';
    }
    return '<ul class="errors">' . implode('', $output) . '</ul>';  
}
//set validation array
function output_valids($no_errors){
    $output = array();
    foreach($no_errors as $no_error){
        $output[] = '<li>' . $no_error . '</li>';
    }
    return '<ul class="valid">' . implode('', $output) . '</ul>';   
}
$no_errors = array();
$errors = array();
if (isset($_FILES['image']) AND $_FILES['image']['error']== 0){
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["image"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
$only_extentios = array('jpg', 'jpeg', 'gif','png');
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["image"]["tmp_name"]);
    if($check !== false) {
        array_push($no_errors,"File is an image - " . $check["mime"] . ".");
        $uploadOk = 1;
    } else {
        array_push($errors,"File is not an image.");
        $uploadOk = 0;
    }
}
// check for correct image extention and size
if (!in_array($imageFileType,$only_extentios)){
    array_push($errors,"Sorry, only jpg, jpeg, png & gif files are allowed.");
    $uploadOk = 0;
}elseif ($_FILES["image"]["size"] > 10000000){
    array_push($errors,"Sorry, your file is too large.");
    $uploadOk = 0;      
}
// Check if file already exists, if uploadok is set to one ant try to upload image
if (file_exists($target_file)) {
    array_push($errors,"Sorry, file already exists.");
    $uploadOk = 0;
}
if($uploadOk == 1){
    move_uploaded_file($_FILES["image"]["tmp_name"], $target_file);
}else{
    array_push($errors,"Sorry, your file was not uploaded.") ;
}
if (file_exists($target_file)){
        array_push($no_errors,"The file ". basename( $_FILES["image"]["name"]). " has/had been uploaded.");
    } else {
        array_push($errors,"Sorry, there was an error uploading your image.");
    }
}
?>  
<!DOCTYPE html>
<html lang="fr"> 
<head>
      <meta charset="utf-8"/>
      <title > image upload </title>
      <meta name="viewport" content="width=device-width, initial-scale=1"/>
      <meta name="author" content="image uploader"/>
      <style type="text/css" >
*,{ 
margin: 0px;
padding: 0px;
font-family: 'Oswald', sans-serif;
}
header,section,footer,aside,nav,article,hgroup { 
display: block;
}
body{ 
width:100%; color:black;
display:-webkit-box;
-webkit-box-pack: center; 
-webkit-box-orient:vertical;
-webkit-box-flex: 1;
background: rgba(204,204,255,0.9);
  background-repeat:repeat;
}
.errors{
    width:97%;
    height:auto;
    float:left;
    margin-left:3%;
    padding:10px;
}
.errors li{
    text-align:left;
    color:red;
    font-size:15px;
    list-style:;
}

.valid{
    width:97%;
    height:auto;
    float:left;
    margin-left:3%;
    padding:10px;
}
.valid li{
    text-align:left;
    color:green;
    font-size:15px;
    list-style:;
}
</style>
      </head>
<body>
   <p><form  action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="multipart/form-data" >
   <?php
    echo output_valids($no_errors);
    ?>
   Sélectionnez une photo:
   <label class="custom-file-upload" style=" margin:0px auto;">  <input type="file" name="image" /> choix </label>
    <input type="submit" value="Envoyer " name="submit"/>

    <?php
    echo output_errors($errors);
    ?>
    </form></p>
</body>
</html>


Here is a quick tutorial I found for doing a file upload:

http://www.tizag.com/phpT/fileupload.php

I read through it pretty quick, but it gives you the basic idea with some safety stuff to boot


specify your directory path here

$target_path = "uploads/";

replace the name of file input with uploadedfile and rest code will work fine

`

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {

echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
" has been uploaded";

} else{

echo "There was an error uploading the file, please try again!";

}

`


As Your Code below is working example:

first create a folder with name "myfolder" in your htdocs folder

[img_uploader.php]

<?php
$target_path = "myfolder/";

$target_path = $target_path . basename( $_FILES['fileToUpload']['name']); 

if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['fileToUpload']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
?>


In the working directory is fine work upload but, uploading in the database at the same time is note working any one solve this problem? kindly.

function doInsert(){ if(isset($_POST['save'])){ if ( $_POST['BRANCHNAME'] == "" || $_POST['BRANCHLOCATION'] == "" || $_POST['BRANCHCONTACTNO'] == "" ) { $messageStats = false; message("All field is required!","error"); redirect('index.php?view=add'); }else{
$branch = New Branch(); $branch->BRANCHNAME = $_POST['BRANCHNAME']; $branch->BRANCHLOCATION = $_POST['BRANCHLOCATION']; $branch->BRANCHCONTACTNO = $_POST['BRANCHCONTACTNO']; $branch->BRANCHLEVEL = $_POST['BRANCHLEVEL']; $branch->BRANCHSTATUS = $_POST['BRANCHSTATUS']; $branch->BRANCHMANAGER = $_POST['BRANCHMANAGER']; $branch->BRANCHDESCRIPTION = $_POST['BRANCHDESCRIPTION']; //$branch->PICLOCATION = $_POST['PICLOCATION'];

        $branch->_FILES       = $_POST['PICLOCATION'];

        $file =$_FILES['PICLOCATION'];

        $filename = $file['name'];
        $filepath = $file['tmp_name'];
        $fileerror = $file['error'];

        if ($fileerror == 0) {
            $destfile = 'photos/'.$filename;
            move_uploaded_file($filepath, $destfile);


        
        

        $branch->LATITUDE                       = $_POST["LATITUDE"];
        $branch->LONGITUDE                      = $_POST["LONGITUDE"];
        $branch->create();

        message("New Branch created successfully!", "success");
        redirect("index.php");
        
    }
    }

} }

0

精彩评论

暂无评论...
验证码 换一张
取 消