2010-04-16 60 views
-2

如何製作基本的PHP上傳器?我想我的圖片在我htdocs/myfolder/
php image upload

這裏保存是我的代碼:

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

plz send me teh codez – 2010-04-16 19:40:09

回答

2

我跟着這個教程,使我的第一個PHP圖片上傳前段時間:

File Upload Tutorial

+1

+1。真的,你所要做的就是谷歌。 – 2010-04-16 19:37:28

+0

move_uploaded_file($ _FILES [「fileToUpload」] [「tmp_name」], 「C:/ program files/xampp/htdocs/jordan pagaduan/user_images /」。 .....我有這個代碼...但我想我的目錄只是/ user_images/...不包括驅動器.. – Jorge 2010-04-16 19:44:20

0

下面是一個簡單的教程,我發現做一個文件上傳:

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

我通過它相當快讀,但它爲您提供了一些安全的東西的基本思想來引導

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>