2015-10-14 104 views
0

我試圖將圖像放在我的文件夾從「$ tablename」生成的位置,但無法將圖像帶到那裏。如何存儲上傳的文件?使用mkdir特定位置上傳文件和圖像

我喜歡從我的表單上傳圖片。

<input type="file" id="file" name="files" multiple /> 

無論使用哪種上傳技術都不好用,要將文件保存到服務器上的特定位置。

如果你有一個想法如何解決這個問題,請。

這裏是mkdir的代碼。代碼工作正常。

<?php 
$tablename = "fisa"; 
$next_increment = 0; 
$qShowStatus = "SHOW TABLE STATUS LIKE '$tablename'"; 
$qShowStatusResult = mysql_query($qShowStatus) or die("" . mysql_error() . "" . $qShowStatus); 

$row = mysql_fetch_assoc($qShowStatusResult); 
$next_increment = $row['Auto_increment']; 

echo "$next_increment"; 

$tablename = (string) ("$next_increment"); 

$year = date("Y"); 
$month = date("m"); 
$day = date("d"); 
If (!file_exists($year)) { 
    $createsyear = mkdir("$year", 0777); 
} else { 
    If (!file_exists("$year/$month")) { 
     $createsmonth = mkdir("$year/$month", 0777); 
    } else { 
     If (!file_exists("$year/$month/$day")) { 
      $createsday = mkdir("$year/$month/$day", 0777); 
     } else { 
      If (!file_exists($year/$month/$day/$tablename)) { 
       $createsday = mkdir("$year/$month/$day/$tablename", 0777); 
      } else { 
       //dada 
      } 
     } 
    } 
} 
?> 

謝謝。

+0

那麼爲什麼'java'標籤? – Satya

+0

去用java嗎? – razvan

+0

你應該瞭解'move_uploaded_file'函數。 http://php.net/manual/en/function.move-uploaded-file.php – urfusion

回答

0

我解決這個問題

<?php 
    $tablename  = "fisa"; 
    $next_increment  = 0; 
    $qShowStatus  = "SHOW TABLE STATUS LIKE '$tablename'"; 
    $qShowStatusResult = mysql_query($qShowStatus) or die ("" . mysql_error() . "" . $qShowStatus); 

    $row = mysql_fetch_assoc($qShowStatusResult); 
    $next_increment = $row['Auto_increment']; 

    echo "$next_increment"; 

    $tablename = (string)("$next_increment"); 
    $year = date("Y"); 
    $month = date("m"); 
    $day = date("d"); 

    If(!file_exists($year)){ 
    $createsyear = mkdir("$year", 0777); 
    } 
    else 
    { 
    If(!file_exists("$year/$month")){ 
    $createsmonth = mkdir("$year/$month", 0777); 
    } 
    else 
    { 
    If(!file_exists("$year/$month/$day")){ 
    $createsday = mkdir("$year/$month/$day", 0777); 
    } 
    else 
    { 
    If(!file_exists($year/$month/$day/$tablename)){ 
    $createsday = mkdir("$year/$month/$day/$tablename/", 0777); 



    $valid_formats = array("jpg", "png", "gif", "zip", "bmp"); 
    $max_file_size = 1024*100; //100 kb 

    $target = "$year/$month/$day/$tablename/"; 
    $count = 0; 

    if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){ 
     // Loop $_FILES to execute all files 
     foreach ($_FILES['files']['name'] as $f => $name) {  
      if ($_FILES['files']['error'][$f] == 4) { 
       continue; // Skip file if any error found 
      }   
      if ($_FILES['files']['error'][$f] == 0) {    
       if ($_FILES['files']['size'][$f] > $max_file_size) { 
        $message[] = "$name is too large!."; 
        continue; // Skip large files 
       } 
       elseif(! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)){ 
        $message[] = "$name is not a valid format"; 
        continue; // Skip invalid file formats 
       } 
       else{ // No error found! Move uploaded files 
        if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $target.$name)) { 
         $count++; // Number of successfully uploaded files 
        } 
       } 
      } 
     } 
    } 

    } 
    else 
    { 

    } 
    } 
    } 


    } 


    ?> 
+0

現在出現了另一個問題,生成一個數字如450.想要一個-449不生成450一個減號。有可能的 ?當我創建一個文件時,它會生成兩個文件,一個450和451,我不想要這個文件。該ID從我的MySQL表格中獲取 – razvan

1

這裏我給出了文件上傳的基本示例。

在HTML

<form action="phpfilename.php" method="post" enctype="multipart/form-data" > 
<input type="file" name="files" /> 
<input type="submit" name="submit" /> 
</form> 

在PHP

<?php 
    if(isset($_POST['submit'])) 
    { 
    $path = $_FILES["files"]["name"]; 
    $target = "$year/$month/$day/$tablename/$path"; //this is your path where image need to be saved 
    move_uploaded_file($_FILES["files"]["tmp_name"], $target); 
    } 
?> 
+0

不工作,我把它放在value =「...」???是java還是php? – razvan

+0

這是PHP。你的代碼只是PHP。 @razvan'價值= ??'你是什麼意思? –

+0

」/> – razvan