php将pdf文件存入数据库,使用PHP保存PDF文件

將PDF文件保存到服務器上的文件夾時出現問題。代碼一次工作,現在不行。我想要做的是在提交表單時檢查是否有人嘗試上載PDF,如果文件字段中有PDF,則上傳它,然後將路徑保存到mysql數據庫。代碼如下:使用PHP保存PDF文件

if (!empty($_FILES['pdf'])){

$idir = "../files/PDF/"; //my directory file is supposed to be saved in

$randomd=rand(0000000,9999999); //creates a random number as filename

$domain = "http://".$_SERVER['HTTP_HOST'];

$file_ext = strrchr($_FILES['pdf']['name'], '.'); grabs file extension. my code checked if the file was a pdf a different way and neither seems to work.

$destination=$randomd.$file_ext; //new filename

if ($file_ext=='pdf') {

move_uploaded_file($_FILES['pdf']['tmp_name'], "$idir" . $destination);

$pdf= $domain."/files/PDF/".$destination; } else { echo("File type not supported.");

mysql_query("UPDATE tbl_listings SET pdf='$pdf' WHERE listing_id='$lid'");

}

的如果不爲空不工作,它總是試圖上傳一個文件,但是當我檢查的文件夾沒有在那裏,它不更新了MySQL。

+1

'move_uploaded_file()'返回什麼? –

+0

@yuvin沒什麼。 –

+0

該函數返回TRUE或FALSE(在某些情況下,它會發出警告)。請參考[documentation](http://php.net/manual/en/function.move-uploaded-file.php)。我認爲原因可能是編寫權限或文件大小。也嘗試使用絕對目標路徑。 –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值