2013-08-28 105 views
0

我正在爲我的網站創建管理員端。使用php存儲圖像到目錄

我已經創建了一個添加頁面,並且在此頁面中是一個包含幾個字段的表單。其中2個是我上傳圖片的上傳按鈕。另一個是我輸入URL的填充框。

我已經設置了它們,以便它們都更新相同的mysql字段,並且填充框也是優先的,當兩者都輸入時。

但是,搜索框是好的,因爲它正確保存的鏈接,但選擇使用選擇框的圖像僅在這一領域在mysql中保存的文件名,例如:xxxxxxxxxxxxx.png

因此,與my website顯示這些圖像,它不顯示上傳的。顯然,因爲這些保存在我的電腦上,而不是我的cPanel。

所以我的問題是:如何 可以通過上傳框我上傳的圖片,並把它保存到我的/圖像文件夾,然後有mysql顯示在相關字段的URL鏈接?

我add.php代碼是這樣的:

<?php 

session_start(); 

include_once('../include/connection.php'); 

if (isset($_SESSION['logged_in'])){ 
    if (isset($_POST['title'], $_POST['content'])) { 
     $title = $_POST['title']; 
     $content = nl2br($_POST['content']); 
     if (!empty($_POST['image'])) 
     { 
      $image = $_POST['image']; 
     } 
     else 
     { 
      $image = $_POST['imageupload']; 

      if (isset($_FILES['Filedata'])) 
      { 
       $filename = $_FILES['Filedata']['name']; 
       $targetpath = "/images/" . $filename; //target directory relative to script location 

       $copy = copy($_FILES['Filedata']['tmp_name'], $targetpath); 

       if (!$copy) 
        $error = "Image was not uploaded successfully"; 
      } 
     } 
     $link  = $_POST['link']; 
     $category = $_POST['category']; 
     $brand = $_POST['brand']; 


if (empty($title) or empty($content)) { 
     $error = 'All Fields Are Required!'; 
}else{ 
$query = $pdo->prepare('INSERT INTO mobi (promo_title, promo_content, promo_image, promo_link, promo_cat, promo_name) VALUES(?, ?, ?, ?, ?, ?)'); 
$query->bindValue(1, $title); 
$query->bindValue(2, $content); 
$query->bindValue(3, $image); 
$query->bindValue(4, $link); 
$query->bindValue(5, $category); 
$query->bindValue(6, $brand); 



    $query->execute(); 
    header('location: index.php'); 
} 

} 
      ?> 
    <?php 

if (isset($_FILES['Filedata'])) 
{ 
// And if it was ok 
    if ($_FILES['Filedata']['error'] !== UPLOAD_ERR_OK) 
    exit('Upload failed. Error code: ' . $_FILES['image']['error']); 

    $filename = $_FILES['Filedata']['name']; 
    $targetpath = "../img/news/" . $filename; //target directory relative to script location 

    $copy = copy($_FILES['Filedata']['tmp_name'], $targetpath); 
} 
?> 

<html> 
<head> 
<title>Add Article</title> 
<link rel="stylesheet" href="../other.css" /> 
</head> 

<body> 
<div class="container"> 
<a href="index.php" id="logo"><b>&larr; Back</b></a> 

<br /> 

<div align="center"> 
<h4>Add Article</h4> 

<?php if (isset($error)) { ?> 
    <small style="color:#aa0000;"><?php echo $error; ?></small><br /><br /> 
<?php } ?> 

<form action="add.php" method="post" autocomplete="off"> 

<input type="text" name="title" placeholder="Title" /><br /><br /> 
<textarea rows="15" cols="50" placeholder="Content" name="content"></textarea><br /><br /> 
<input name="imageupload" type="file" id="image" placeholder="Imageupload" /> 
<input type="text" name="image" placeholder="Image" /><br /><br /> 
<input type="link" name="link" placeholder="Link" /><br /><br /> 
<input type="category" name="category" placeholder="Category" /><br /><br /> 
<input type="category" name="brand" placeholder="Brand" /><br /><br /> 
<input type="submit" value="Add Article" /> 

</form> 
</div> 
</div> 
</body> 
</html> 


<?php 
}else{ 
     header('location: index.php'); 
} 

?> 

請幫助。謝謝。

回答

0

首先必須使用的輸入字段與表單類型=「文件」和ENCTYPE =「的multipart/form-data的」獲取文件。 確認後,你可以做:

move_uploaded_file ($_FILES['name']['tmp_name'] , $destination) 
+0

這需要輸入哪裏? – kevstarlive

+0

我是否將上面的代碼替換爲$ copy = copy($ _ FILES ['Filedata'] ['tmp_name'],$ targetpath); – kevstarlive

+0

當您提交表單時,您會在服務器中獲得$ _FILES []。然後,您可以使用$ _FILES ['yourInputName'] ['name']驗證文件的名稱。用$ _FILES ['yourInputName'] ['error'],用$ _FILES ['yourInputName'] ['size']驗證不同的錯誤,驗證尺寸文件。然後,您可以使用函數move_uploaded_file()將文件上載到服務器中的任何目錄。之後,您可以使用該文件將其發送給客戶端。 – Manolo