我的代码遇到了问题。我知道我做错了什么,但我不知道是什么。 我有一张照片上传表单,如果它不存在,它必须创建一个文件夹,并将图片上传到该文件夹。同时创建文件夹并上传文件
所以我的问题是,我不能在同一时间做这两个 e。 如果该文件夹已经存在,图像将被上传,但是如果文件夹不存在,它只会创建文件夹,而我必须为该图片进行另一次上传才能进入该文件夹。
这是我的代码。
INDEX.HTML
<form action="" method="post" enctype="multipart/form-data">
<input id="input-image-3" class="file-loading" type="file" id="file" name="files[]" multiple="multiple" accept="image/*" />
</form>
<script>
$("#input-image-3").fileinput({
uploadUrl: "upload.php",
allowedFileExtensions: ["jpg","jpeg", "png", "gif"],
maxImageWidth: 100,
maxImageHeight: 100,
resizePreference: 'height',
maxFileCount: 10,
resizeImage: true,
}).on('filepreupload', function() {
$('#kv-success-box').html('');
}).on('fileuploaded', function(event, data) {
$('#kv-success-box').append(data.response.link);
$('#kv-success-modal').modal('show');
});
</script>
upload.php的
<?php
$valid_formats = array("jpg", "png");
$max_file_size = 1024*100; //100 kb
$path = "up/test/"; // Upload directory, later i'll get the name of user
$filename=$file['filename'];
$extension = '.jpg';
$i=1;
while(file_exists($path.$filename.$extension)){
$filename=$file['filename']."teste_($i)";
$i++;}
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
// Loop $_FILES to exeicute all files
foreach ($_FILES['files']['name'] as $f => $name) {
if ($_FILES['files']['error'][$f] == 4) {
continue; // Skip file if any error found
}
if ($_FILES['files']['error'][$f] == 0) {
if ($_FILES['files']['size'][$f] > $max_file_size) {
$message[] = "$name is too large!.";
continue; // Skip large files
}
elseif(! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)){
$message[] = "$name is not a valid format";
continue; // Skip invalid file formats
}
else{ // No error found! Move uploaded files
if(!is_dir($path)){mkdir($path, 0755);} // If dont exist, create folder
move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$filename.$extension);
}
}
}
}
echo json_encode (json_decode ("{}"));
?>
谢谢你在前进,
尝试$ oldumask =的umask(0); mkdir($ path,0777,TRUE);的umask($ oldumask);我用这个技巧 –
你好,@SameerJain。感谢您的帮助,我试图添加该代码,但不幸的是,这并不起作用。它在那时只是一个动作。 – grcoder