Php 如果未选择任何文件,则不执行任何操作
这很有效。问题是,如果用户没有选择一个文件,就会出现错误。如果未选择任何文件,我如何使其不检查任何内容?如果选择了文件,则仅检查错误 此函数位于Php 如果未选择任何文件,则不执行任何操作,php,Php,这很有效。问题是,如果用户没有选择一个文件,就会出现错误。如果未选择任何文件,我如何使其不检查任何内容?如果选择了文件,则仅检查错误 此函数位于中,在提交表单时运行 if (empty($errors) === true){ if (isset($_FILES['cover']) === true){ $allowed = array('jpg', 'jpeg', 'gif', 'png'); $file_name = $_FILES['cover']['
中,在提交表单时运行
if (empty($errors) === true){
if (isset($_FILES['cover']) === true){
$allowed = array('jpg', 'jpeg', 'gif', 'png');
$file_name = $_FILES['cover']['name'];
$file_size = getimagesize($_FILES['cover']['name']);
$file_extn = strtolower(end(explode('.', $file_name)));
$file_temp = $_FILES['cover']['tmp_name'];
if(in_array($file_extn, $allowed) === true){
$file_path = 'images/cover/' . substr(md5(time()), 0, 10) . '.' . $file_extn;
move_uploaded_file($file_temp, $file_path);
}else{
$errors[] = 'Incorrect file type. Only allowed: ' . implode(', ', $allowed) . '';
}
}
也许这样的事情对你有用
if (!empty($_FILES['cover'])){
if (empty($errors) === true){
if (isset($_FILES['cover'])){
$allowed = array('jpg', 'jpeg', 'gif', 'png');
$file_name = $_FILES['cover']['name'];
$file_size = getimagesize($_FILES['cover']['name']);
$file_extn = strtolower(end(explode('.', $file_name)));
$file_temp = $_FILES['cover']['tmp_name'];
if(in_array($file_extn, $allowed) === true){
$file_path = 'images/cover/' . substr(md5(time()), 0, 10) . '.' . $file_extn;
move_uploaded_file($file_temp, $file_path);
}else{
$errors[] = 'Incorrect file type. Only allowed: ' . implode(', ', $allowed) . '';
}
}
}
测试它或其他数据是否设置为nothing如何
if (!empty($_FILES['cover'])) {
if ($_FILES['cover']['name'] != "") {
// execute your code here
} else if ($_FILES['cover']['name'] == "") {
// do nothing
// or header them to the appropriate page
header("location: http://yourpage.com");
exit();
}
}
我添加这个是为了看看我们是否在同一页上
if (!empty($_FILES['cover'])){
if ( $_FILES['cover']['name'] != "") {
$file_name = $_FILES['cover']['name'];
// etc
// then the rest of your code
} else {
// do nothing
}
} else {
// do nothing or header to appropriate location and exit
header("location: http://yourpage.com");
exit();
}
好的,试试这个:
if(!empty($_FILES['cover'])) {
if($_FILES['cover']['tmp_name'] != "") {
$fileName = $_FILES["cover"]["name"];
$fileTmpLoc = $_FILES["cover"]["tmp_name"];
$fileType = $_FILES["cover"]["type"];
$fileSize = $_FILES["cover"]["size"];
$fileErrorMsg = $_FILES["cover"]["error"];
$boomBoom = explode(".", $fileName);
$fileExt = end($boomBoom);
// this next if is okay because if a new file is not selected
// the script wouldn't make it this far anyway
// so this will not require a new selection
// just a check that should pass every time
if (!$fileTmpLoc){
echo "ERROR: Please browse for a file before uploading";
exit();
}
else if($fileSize > 20480000){
echo "ERROR: Your file was larger than 20 Megabytes";
unlink($fileTmpLoc);
exit();
}
else if (!preg_match("/.(gif|jpg|png|jpeg)$/i", $fileName)){
echo "ERROR: Your image was not .gif, .jpg, .jpeg, or .png.";
unlink($fileTmpLoc);
exit();
}
else if ($fileErrorMsg == 1){
echo "ERROR: An error occurred.";
exit();
}
move_uploaded_file($_FILES['cover']['tmp_name'], "pathTo/$fileName");
header("location: http://www.yoursite.com");
exit();
}
}
您是否每次都要求提交文件?还是可选?无关:
isset()
始终返回布尔值。您不必检查==true
甚至==true
。这样就可以了:if(isset($\u FILES['cover']){…
@les这是可选的,我尝试了空($\u FILES['cover']),但这只是说,如果它是空的,显示错误,它不会说如果空就忽略它。不会死()简单地关闭页面?@bamba检查我下面的答案。如果这不起作用,让我知道-我有一个工作脚本,我可以拉出来。你如何告诉它“什么都不做”@bamba,不写任何东西。然后你exit();
。你真的不必把else-If条件放进去。简单的else就可以了。else{exit();}
edit:它确实有效,但在退出时不会提交任何内容()。还有其他方法吗?解决了它!只需删除“其他”就可以了!感谢所有的帮助help@bamba是的,除非你打算重定向或刷新页面等,否则你不会想退出。我如何让它保持相同的图像(编辑帖子时)如果未选择任何新内容?将数据复制到一个隐藏字段中,然后在发布表单时与这两个字段进行比较,以确定是否发生了更改。您能告诉我怎么做吗?我的意思是,如何将旧上传的文件与新上传的文件(或空文件)进行比较?您能帮助我吗?