文件有时不会上传到服务器

时间:2019-05-28 16:52:09

标签: php html

我有一些php代码,可以将文件上传到从html表单接收的选定服务器目录(media/website_uploads/)中。

它可以在90%的时间内工作,但是有时它不能创建文件,我找不到在代码中尝试使用不同类型的文件,不同大小但找不到错误原因的原因。任何帮助将不胜感激。

//Check if has extension if not define it as jpg as default
if (strrpos(basename($_FILES['fileuploaded']['name']), '.') == 0) {
    $extensao = 'jpg';
} else {
    $extensao = substr(basename($_FILES['fileuploaded']['name']),strrpos(basename($_FILES['fileuploaded']['name']), '.') + 1);
}

//Gets the time stamp and creates new file name
$timeAux = $date; //date has current time
$timeAux = str_replace(':', '', $timeAux);
$name_of_uploaded_file1 =  'atls_' . date('Y-m-d') . '_'  . rand(0,999999). $timeAux .'.'.$extensao;
$errors = '';
//Server directory where to sabe
$upload_folder = 'media/website_uploads/';
$type_of_uploaded_file = substr($name_of_uploaded_file1,strrpos($name_of_uploaded_file1, '.') + 1);
$size_of_uploaded_file = $_FILES["fileuploaded"]["size"]/1024;//size in KBs
//Settings
$max_allowed_file_size = 8000; // size in KB
$allowed_extensions = array("jpg", "jpeg", "gif", "png", "bmp", "doc", "docx", "pdf", "xls", "xlsx", "zip", "rar", "tif");
//Validations
if($size_of_uploaded_file > $max_allowed_file_size ) {
    $errors .= "\n File size must be less than $max_allowed_file_size Kb";
}
//------ Validate the file extension -----
$allowed_ext = false;
for($i=0; $i<sizeof($allowed_extensions); $i++) {
    if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0) {
        $allowed_ext = true; 
    }
}
if(!$allowed_ext && isset($_FILES['fileuploaded']['name']))
{  
    $errors .= "\n File not accepted. ".
                "Only accept the following files: ".
                implode(',',$allowed_extensions);
}
//copy the temp. uploaded file to uploads folder
$path_of_uploaded_file = $upload_folder . $name_of_uploaded_file1;
$tmp_path = $_FILES["fileuploaded"]["tmp_name"];
if(is_uploaded_file($tmp_path)) {
    copy($tmp_path,$path_of_uploaded_file);
}
$fileuploadedURL = $topDomain.$path_of_uploaded_file;

HTML

<label class="label-noPadding" for="fileuploaded">Anexe file</label>
<input type="file" class="form-control fileInputText" name="fileuploaded" id="fileuploaded" required="true">
<small style="margin-top: 5px;">Suported files, "jpg", "jpeg", "gif", "png", "bmp", "doc", "docx", "pdf", "xls", "xlsx", "zip", "rar", "tif", with max size of 3 Mb.</small>

0 个答案:

没有答案