Javascript 在文件选项中选择后,将图像保存到临时文件夹,然后在提交cakephp后将其保存到文件夹
当用户从“文件选项”中选择图片后,我尝试先将其保存到临时文件夹。然后在提交表单后将其保存到移动文件夹 以下是上传代码:Javascript 在文件选项中选择后,将图像保存到临时文件夹,然后在提交cakephp后将其保存到文件夹,javascript,php,cakephp,upload,temporary,Javascript,Php,Cakephp,Upload,Temporary,当用户从“文件选项”中选择图片后,我尝试先将其保存到临时文件夹。然后在提交表单后将其保存到移动文件夹 以下是上传代码: $id = $this->Session->read('Auth.User.id'); $userData = $this->User->findById($id); if ($userData['User']['id'] === $this->Session->read('Auth.User
$id = $this->Session->read('Auth.User.id');
$userData = $this->User->findById($id);
if ($userData['User']['id'] === $this->Session->read('Auth.User.id')) {
$this->set('userData', $userData);
} else {
throw new NotFoundException(__('You are not allowed to this page!'));
}
move_uploaded_file($fileTmp, WWW_ROOT.'files\Users'.DS.$uid.DS.$fileInfo['tmpFileName']);
此代码允许我直接将其保存到文件夹中,而无需将其保存到临时文件夹中 我想保存正在预览的内容 以下是我的javascript预览:
//preview
$("#file").change(function() {
var file = this.files[0];
var imagefile = file.type;
var imagesize = file.size;
var match= ["image/jpeg","image/png","image/jpg","image/gif"];
$('.upload-submit').prop('disabled',false).css('opacity',1);
if(!((imagefile==match[0]) || (imagefile==match[1]) || (imagefile==match[2]) || (imagefile==match[3])))
{
$("#message").html("<p id='error' style='color:red;'>Please Select A valid Image File</p>"+"<h4 style='color:red;'>Note</h4>"+"<span id='error_message' style='color:red;'>Only jpeg, jpg, gif and png Images type allowed</span>");
return false;
} else if (imagesize > 6000000){
$("#message").html("<p id='error' style='color:red;'>Your file size is higher than the allowed size (6MB)</p>");
return false;
}
else
{
var reader = new FileReader();
reader.onload = imageIsLoaded;
reader.readAsDataURL(this.files[0]);
var imgData = reader.onload; }
});
function imageIsLoaded(e) {
$("#file").css("color","green");
$('#image_preview').css("display", "block");
$('#previewing').attr('src', e.target.result);
$('#previewing').attr('width', '250px');
$('#previewing').attr('height', '230px');
};
和我的HTML:
<form method="post" action="/users/profileimg_edit" enctype="multipart/form-data" id="uploadimage">
<?php echo $this->Session->flash(); ?>
<input type="file" name="file" id="file" class="upload-image-btn" >
<div id="image_preview"><img id="previewing" src="/images/noimage.png" width="80" height="80" />
<div id="message"></div>
</div>
<button class="upload-submit"> Upload Image </button>
</form>
使用此代码,用户可以查看他/她选择了什么并将其保存,但我希望先将其保存在临时文件夹中
感谢您提供的帮助。首先将图片移动到服务器上的临时目录,例如WWW\u ROOTtmp’。DS$fileInfo['tmpFileName'],然后在预览中使用该路径,使URL变得类似http://my.server.com/tmp/b5c8f033dfaa.jpg. 然后,当用户完成预览后,将文件移动到最终目标 但是,您必须跟踪临时文件名$fileInfo['tmpFileName'],因为如果转到下一页,该信息将丢失。您可以将其放入URL或存储在会话对象中
您可能还希望添加cronjob以从tmp目录中删除旧上载的文件,因为通常会有一些被放弃的文件上载等。此代码允许我将其直接保存到文件夹中,而无需将其保存到temp文件夹中。使用此代码,用户可以查看他/她选择了什么并保存它,但我想先将其保存在临时文件夹中???