PHP 问题:如何把上传文件这个功能改为选择性的?目前是必须的,谢谢

2014-03-03 12:59:31 +08:00
 Wao
我自己加了个很简单的判断,但是不对,文件上传不了了,也没错误提示
在线等,谢谢!

code:
======================================
<?php
include ('../header.php');
include ('../secure_page.php');
include ('../php/phpClass/data_class.php');

$userID=$_SESSION['user_id'];
$user=$_SESSION['username'];

$dest_dir="../media/$user/".date(Ymd);

if( !is_dir($dest_dir) || !is_writeable($dest_dir) )
{
if(!mkdir($dest_dir, 0777, true))
die("The expected directory ".$dest_dir." encountered errors");
}

$type=array("png","jpg","jpeg");

$upfile=$_FILES['file'];

if($upfile != NULL)
{

function fileext($filename)
{
return substr(strrchr($filename, '.'), 1);
}

if( !in_array( strtolower( fileext($upfile['name'] ) ),$type) )
{
$text=implode(",",$type);
echo "Sorry, you cannot upload this file type: ",$text;
}
else
{
$dest=$dest_dir.'/'.date("His")."_".$upfile['name'];

$state=move_uploaded_file($upfile['tmp_name'],$dest);

if ($state)
{
print("File Uploaded. Thanks for your contribution!");
}
else
{
switch($upfile['error'])
{
case 1 : die("File size is larger than php.ini: upload_max_filesize");
case 2 : die("File is too large");
case 3 : die("Only part of file has been uploaded");
case 4 : die("No file has been uploaded");
case 5 : die("Cannot find the expected directory");
case 6 : die("File cannot be written");
}
}
}
}
else
{
print("Thanks for your contribution!");
}

$aname = $_POST['aname'];
$atime = $_POST['atime'];
$adate = $_POST['adate'];
$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
$agchoice = $_POST['agchoice'];
$age = $_POST['age'];
$aschoice = $_POST['aschoice'];
$adescription = $_POST['adescription'];

$dataClass = new Data();
$data = array($aname,$atime,$adate,$latitude,$longitude,$agchoice,$aschoice,$adescription,$age,$dest);

$h = $dataClass->add_sighting($data, $userID);


?>
==========================
2903 次点击
所在节点    问与答
2 条回复
vcex
2014-03-07 10:30:32 +08:00
目测,$dest变量保存了上传文件的保存路径。当没有文件被上传时候, $dest并没有被定义,所以在函数$dataClass->add_sighting ()中 $data的长度不够,
解决方法:
else
{
print("Thanks for your contribution!");
}
里面加上一句对 $dest的赋值
Wao
2014-03-07 12:05:43 +08:00
@vcex 谢谢~ 不过应该不是您说的原因

我把最开始对文件是否为空的判断改为 if( $_FILES['file']['name'] != "" ) 后,问题解决了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/102639

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX