我的圖片上傳表單工作正常。保存數據庫的名稱和路徑。我想要保存完整的網址,而不是僅僅使用名稱來形象。我的意思是現在在DB中保存file_name.jpg
我想要保存http://example.com/images/file_name.jpg
。這裏是upload.php
當圖片上傳時在數據庫中保存完整的URL鏈接
define('MAX_FILE_SIZE', 20000000430);
$uploadDir = "../../img/";
$permitted = array('image/jpeg', 'image/jpeg', 'image/png', 'image/gif');
$fileName = $_FILES['image']['name'];
$tmpName = $_FILES['image']['tmp_name'];
$fileSize = $_FILES['image']['size'];
$fileType = $_FILES['image']['type'];
// make a new image name
$ext = substr(strrchr($fileName, "."), 1);
// generate the random file name
$randName = md5(rand() * time());
// image name with extension
$myFile = $randName . '.' . $ext;
// save image path
$path = $uploadDir . $myFile;
if (in_array($fileType, $permitted))
{
$result = move_uploaded_file($tmpName, $path);
if (!$result)
{
echo "Error uploading image file";
exit;
}
else
{
// keep track post values
$name = $_POST['name'];
$description = $_POST['description'];
// update data
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE food set name = ?, image = ?, path = ?, description = ?
WHERE id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($name,$myFile,$path,$description,$id));
Database::disconnect();
echo "<code>Information updated!!</code>";
}
}
我試着把URL放在$ uploadDir中。
$uploadDir = "http://example.com/img/";
但我得到這個錯誤。
Warning: move_uploaded_file(http://example.com/img/75a13564a8f3305fb0a30ab95487b8de.jpg): failed to open stream: HTTP wrapper does not support writeable connections
也試過這樣的事情,得到了同樣的錯誤
define('domainURL', 'http://'.$_SERVER['HTTP_HOST']);
$path = domainURL . $uploadDir . $myFile;
不move_uploaded_file把全名,當你在數據庫中插入鏈接,然後創建完整的url並保存在db – Zeeshan 2014-12-08 06:52:35
如果我正確理解你的意思是什麼時候重寫'$ myFile'具有完整的URL時插入分貝? – 2014-12-08 07:04:48