2010-08-18 50 views
37

tôi đang sử dụng mã này để upload file (hình ảnh vào một thư mục)Làm thế nào để tải lên & Lưu tập tin với mong muốn tên

<form action='' method='POST' enctype='multipart/form-data'> 
<input type='file' name='userFile'><br> 
<input type='submit' name='upload_btn' value='upload'> 
</form> 

<?php 
$target_Path = "images/"; 
$target_Path = $target_Path.basename($_FILES['userFile']['name']); 
move_uploaded_file($_FILES['userFile']['tmp_name'], $target_Path); 
?> 

khi tập tin (hình ảnh) được lưu tại đường dẫn cụ thể ... GÌ nếu tôi muốn lưu các tập tin với một số tên mong muốn ....

tôi đã cố gắng thay thế

$target_Path = $target_Path.basename($_FILES['userFile']['name']); 

NÀY vỚI NÀY

$target_Path = $target_Path.basename("myFile.png"); 

NHƯNG nó không làm việc

+0

gì chính xác là "không làm việc"? Bất kỳ thông báo lỗi nào? – deceze

+3

Bạn không cần 'basename' nếu bạn chỉ sử dụng' "myFile.png" '. Tuy nhiên, không nên tạo sự khác biệt. – qmega

Trả lời

80

Bạn có thể thử này,

$info = pathinfo($_FILES['userFile']['name']); 
$ext = $info['extension']; // get the extension of the file 
$newname = "newname.".$ext; 

$target = 'images/'.$newname; 
move_uploaded_file($_FILES['userFile']['tmp_name'], $target); 
+0

Rất tốt @Manie –

7

Điều này sẽ hoạt động rất tốt - Bạn có thể sử dụng HTML5 để chỉ cho phép tải lên các tệp hình ảnh. Đây là mã cho uploader.htm -

<html>  
    <head> 
     <script> 
      function validateForm(){ 
       var image = document.getElementById("image").value; 
       var name = document.getElementById("name").value; 
       if (image =='') 
       { 
        return false; 
       } 
       if(name =='') 
       { 
        return false; 
       } 
       else 
       { 
        return true; 
       } 
       return false; 
      } 
     </script> 
    </head> 

    <body> 
     <form method="post" action="upload.php" enctype="multipart/form-data"> 
      <input type="text" name="ext" size="30"/> 
      <input type="text" name="name" id="name" size="30"/> 
      <input type="file" accept="image/*" name="image" id="image" /> 
      <input type="submit" value='Save' onclick="return validateForm()"/> 
     </form> 
    </body> 
</html> 

Bây giờ mã cho upload.php -

<?php 
$name = $_POST['name']; 
$ext = $_POST['ext']; 
if (isset($_FILES['image']['name'])) 
{ 
    $saveto = "$name.$ext"; 
    move_uploaded_file($_FILES['image']['tmp_name'], $saveto); 
    $typeok = TRUE; 
    switch($_FILES['image']['type']) 
    { 
     case "image/gif": $src = imagecreatefromgif($saveto); break; 
     case "image/jpeg": // Both regular and progressive jpegs 
     case "image/pjpeg": $src = imagecreatefromjpeg($saveto); break; 
     case "image/png": $src = imagecreatefrompng($saveto); break; 
     default: $typeok = FALSE; break; 
    } 
    if ($typeok) 
    { 
     list($w, $h) = getimagesize($saveto); 
     $max = 100; 
     $tw = $w; 
     $th = $h; 
     if ($w > $h && $max < $w) 
     { 
      $th = $max/$w * $h; 
      $tw = $max; 
     } 
     elseif ($h > $w && $max < $h) 
     { 
      $tw = $max/$h * $w; 
      $th = $max; 
     } 
     elseif ($max < $w) 
     { 
      $tw = $th = $max; 
     } 

     $tmp = imagecreatetruecolor($tw, $th);  
     imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h); 
     imageconvolution($tmp, array(// Sharpen image 
      array(−1, −1, −1), 
      array(−1, 16, −1), 
      array(−1, −1, −1)  
     ), 8, 0); 
     imagejpeg($tmp, $saveto); 
     imagedestroy($tmp); 
     imagedestroy($src); 
    } 
} 
?> 
0

này sử dụng cho đường dẫn đích để tải

<?php 
$file_name = $_FILES["csvFile"]["name"]; 
$target_path = $dir = plugin_dir_path(__FILE__)."\\upload\\". $file_name; 
echo $target_path; 
move_uploaded_file($_FILES["csvFile"]["tmp_name"],$target_path. $file_name); 
?> 
-1

ĐÂY LÀ MÃ TRONG MẪU PHP ĐỂ TẢI LÊN HÌNH ẢNH VÀO DỮ LIỆU, HIỂN THỊ VÀ C ALNG TIẾT KIỆM CHO ĐƠN VỊ ĐỊA PHƯƠNG CỦA BẠN

  1. AT FIRST HTML MÃ CHO MẪU:

    <div class="upload"> 
        <form method="POST" enctype="multipart/form-data" id="imageform"> 
         <br> 
         <input type="file" name="image" id="photoimg" > 
         <br><br> 
         <input type="submit" name="submit" value="UPLOAD"> 
        </form> 
    </div> 
    
  2. ĐÂY LÀ TOÀN BỘ PHP Mã sản phẩm:.

tạo cơ sở dữ liệu và bảng như bạn muốn (chỉ cần 2 lĩnh vực) Trong bảng , id(INT) 255 primary key AUTO INCREMENT and your image row(anyname) (MEDIUMBLOB)

<?php 

if(isset($_POST['submit'])){ 
    if(@getimagesize($_FILES['image']['tmp_name']) == FALSE){ 
     echo "<span class='image_select'>please select an image</span>"; 

    } 
    else{ 
     $image = addslashes($_FILES['image']['tmp_name']); 
     $name = addslashes($_FILES['image']['name']); 
     $image = file_get_contents($image); 
     $image = base64_encode($image); 
     saveimage($name,$image); 
     $uploaddir = 'profile/'; //this is your local directory 
     $uploadfile = $uploaddir . basename($_FILES['image']['name']); 

     echo "<p>"; 

      if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {// file uploaded and moved} 
      else { //uploaded but not moved} 

     echo "</p>"; 



    } 

} 

displayimage(); 
function saveimage($name,$image) 
{ 
    $con = mysql_connect("localhost","root","your database password"); 
    mysql_select_db("your database",$con); 
    $qry = "UPDATE your_table SET your_row_name='$image'"; 
     $result = @mysql_query($qry,$con); 

    if($result) 
    { 
     echo "<span class='uploaded'>IMAGE UPLOADED</span>"; 

    } 
    else 
    { 
     echo "<span class='upload_failed'>IMAGE NOT UPLOADED</span>"; 

    } 
} 
function displayimage() 
{ 
    $con = mysql_connect("localhost","root","your_password"); 
    mysql_select_db("your_database",$con); 
    $qry = "select * from your_table"; 
    $result = mysql_query($qry,$con); 

    while($row = mysql_fetch_array($result)) 
    { 
     echo '<img class="image" src="data:image;base64,'.$row[1].'">'; 

    } 



    mysql_close($con); 
} 
?> 
0

Configure Các "php.ini" file

Trước tiên, hãy đảm bảo rằng PHP được định cấu hình để cho phép tải tệp lên. Trong "php của bạn.ini" tập tin, tìm kiếm các thị file_uploads, và thiết lập nó sang On:

file_uploads = On 

Tạo Mẫu HTML

Tiếp theo, tạo một hình thức HTML cho phép người dùng lựa chọn các tập tin hình ảnh mà họ muốn tải lên:

<!DOCTYPE html> 
<html> 
<body> 

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

</body> 
</html> 

Một số quy tắc để làm theo cho dạng HTML trên: Hãy chắc chắn rằng biểu mẫu sử dụng phương thức = "post" Biểu mẫu cũng cần thuộc tính sau: enctype = "multipart/form-data". Nó chỉ định loại nội dung sẽ sử dụng khi gửi biểu mẫu Nếu không có các yêu cầu ở trên, việc tải lên tệp sẽ không hoạt động. Những điều khác cần lưu ý: Thuộc tính type = "file" của thẻ hiển thị trường nhập dưới dạng điều khiển chọn tệp, với nút "Duyệt" bên cạnh điều khiển nhập Biểu mẫu ở trên gửi dữ liệu đến tệp có tên " upload.php ", mà chúng tôi sẽ tạo tiếp theo.

Tạo Các Upload File PHP Script

Các "upload.php" tập tin chứa mã để tải lên một tập tin:

<?php 
$target_dir = "uploads/"; 
$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"])) { 
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); 
    if($check !== false) { 
     echo "File is an image - " . $check["mime"] . "."; 
     $uploadOk = 1; 
    } else { 
     echo "File is not an image."; 
     $uploadOk = 0; 
    } 
} 
?> 
Các vấn đề liên quan