0
将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。
'move_uploaded_file()'返回什么? – yuvin
@yuvin没什么。 – user1385895
该函数返回TRUE或FALSE(在某些情况下,它会发出警告)。请参考[documentation](http://php.net/manual/en/function.move-uploaded-file.php)。我认为原因可能是编写权限或文件大小。也尝试使用绝对目标路径。 – yuvin