php图片上传

时间:2010-04-16 19:34:21

标签: php

如何制作基本的PHP上传器?我希望我的图片保存在htdocs/myfolder/

这是我的代码:

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

6 个答案:

答案 0 :(得分:2)

我之前按照本教程制作了我的第一个php图片上传器:

File Upload Tutorial

答案 1 :(得分:1)

在PHP网站上有完整的如何执行此操作的示例:

http://us3.php.net/manual/en/features.file-upload.post-method.php

您已经有了一个良好的开端,只需查看该页面上的示例#2。

答案 2 :(得分:0)

以下是我发现的文件上传快速教程:

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

我非常快速地阅读了它,但它为你提供了一些基本的想法,可以用一些安全的东西来启动

答案 3 :(得分:0)

下面的代码是一个简单的例子,涉及图像或文件上传的所有方面,看一看并理解它

<?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>

答案 4 :(得分:0)

在此处指定您的目录路径

$target_path = "uploads/";

用上载的文件替换输入的文件名,其余代码可以正常工作

`

$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!";

}

`

答案 5 :(得分:0)

下面的代码是有效的示例:

首先在htdocs文件夹中创建一个名为“ myfolder”的文件夹

[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!";
}
?>