2013-02-13 72 views
0
<?php 
include('includes/db.php'); 

$drinks_cat = $_POST['drinks_cat']; 
$drinks_name = $_POST['drinks_name']; 
$drinks_shot = $_POST['drinks_shot']; 
$drinks_bottle = $_POST['drinks_bottle']; 
$drinks_availability = 'AVAILABLE'; 

$msg = "ERROR: "; 
$itemimageload="true"; 
$itemimage_size=$_FILES['image']['size']; 
$iname = $_FILES['image']['name']; 
if ($_FILES['image']['size']>250000){$msg=$msg."Your uploaded file size is more than 250KB so please reduce the file size and then upload.<BR>"; 
$itemimageload="false";} 

if (!($_FILES['image']['type'] =="image/jpeg" OR $_FILES['image']['type'] =="image/gif" OR $_FILES['image']['type'] =="image/png")) 
{$msg=$msg."Your uploaded file must be of JPG , PNG or GIF. Other file types are not allowed<BR>"; 
$itemimageload="false";} 

$file_name=$_FILES['image']['name']; 
$add="images"; // the path with the file name where the file will be stored 


if($itemimageload=="true") 
{ 
    if (file_exists($add) && is_writable($add)) 
    { 
     if(move_uploaded_file ($_FILES['image']['tmp_name'], $add."/".$_FILES['image']['name'])) 
     { 
     echo "Image successfully updated!"; 
     } 
     else 
     { 
     echo "Failed to upload file Contact Site admin to fix the problem"; 
     } 
    } 
    else 
    { 
    echo 'Upload directory is not writable, or does not exist.'; 
    } 
} 
else 
{ 
    echo $msg; 
} 

$dir = $add."/".$iname; 
echo "<BR>"; 
// Connects to your Database 



mysql_query("INSERT INTO `product_drinks`(`drinks_id`, `drinks_cat`, `drinks_name`, `drinks_shot`, `drinks_bottle`, `drinks_image`, `drinks_availability`) VALUES (NULL,'".$drinks_cat."', '".$drinks_name."','".$drinks_shot."','".$drinks_bottle."','".$dir."','".$drinks_availability."')") or die("insert error"); 

Print "Your table has been populated"; 
?> 

我正在處理的代碼有效,但我必須爲我的管理文件夾創建一個新的「圖像」文件夾。有沒有辦法,我可以上傳文件管理文件夾外,並將其移動到原來的「圖片」文件夾」。我知道這是相當混亂的任何方式,但我的目錄看起來是這樣的。將圖像上傳到現有文件夾PHP

clubmaru -admin -images

-CSS -images -js

+0

你可以隨意移動它,你喜歡,我不明白的問題 – 2013-02-13 19:07:30

+0

只需使用$添加=「../圖像」;或者使用像(C:\ user \ images)或linux/home/user/public_html/images – 2013-02-13 19:09:19

+1

這樣的完整真實路徑,這個腳本是危險的。它充滿了安全漏洞,把它放在生產系統上會打開服務器,導致完全的遠程危害。請勿使用此代碼。 – 2013-02-13 19:13:12

回答

1

你可能會尋找PHP的重命名功能。http://php.net/manual/en/function.rename.php

將oldname參數設置爲文件(及其路徑),並將newname參數設置爲您希望的位置(顯然與新路徑一起)

只需確保「圖像文件夾」要移動文件具有正確的權限設置確保它是可寫的。您可能還需要考慮改變參數在move_uploaded_file把文件要將它擺在首位!

0

是有一種方法,你需要改變的路徑。現在,您的路徑爲images/$name,這意味着它將將本地目錄中找到的images目錄中的文件放入正在運行的腳本中。

使用目錄佈局:

clubmaru 
    ->admin 
    ->script.php (the upload file) 
    ->images 
    ->css 
    ->images 
     ->js 

你讓路徑的相對(或另找替代)

$add="../css/images"; 

這意味着,去了一個目錄,進入CSS然後進入圖像。

相關問題