I have this jquery code:
$('input[type=file]').on('change', uploadFiles);
function uploadFiles(event) {
var data = new FormData();
var files = event.target.files;
for(var i = 0;i<files.length;i++){
data.append("file_"+i, files[i]);
}
data.append('dir_name',current_directory);
apicall('upload.php','POST',data,fileUploaded);
}
Then in php file:
<?php
$data = array();
$allFiles = scandir($_REQUEST['dir_name']);
if(isset($_FILES))
{
$error = false;
$uploaddir = $_REQUEST['dir_name'];
foreach($_FILES as $file)
{
echo $file['error'];
echo $file['tmp_name'];
$new_name = $file['name'];
move_uploaded_file($file['tmp_name'], $uploaddir .'/'.$file['name']);
}
}
else
{
//some code here
}
echo json_encode($error);
?>
Now this works and everything, I can upload any file and multiple files at a time too. But it causes the memory Firefox uses to skyrocket and never come down again. It is proportional to the file I upload I think, but way way bigger. I read in the manual that move_uploaded_file() deletes the temporary file, so that can't be the problem. The only thing that helps is to close firefox and open it up again. Can somebody please tell me why this happens and how to make it stop?
If it matters, I am using XAMPP to test all this out on localhost.
This does not happen in chrome.
Related
I am using Slim Framework to upload files. I need to upload 10 files in single request of 500MB size. How can I accomplish this. I am using following code for this.
$uploadedFiles = $request->getUploadedFiles();
foreach ($uploadedFiles['aws_file'] as $uploadedFile) {
if ($uploadedFile->getError() === UPLOAD_ERR_OK) {
$uploadFileName = $uploadedFile->getClientFilename();
$fileDetails = pathinfo($uploadedFile->getClientFileName());
$fileName = explode('_',$fileDetails['filename']);
if(count($fileName)==3) {
$orgIdArray[] = $fileName[1];
}
} else {
$responseObj->status = 'error';
$responseObj->message = 'Error in file or file is empty ';
$responseObj->errorFileList[] = $uploadedFile->getError();
}
}
I am getting memory issue.
Increase your memory_limit PHP setting.
I'm having a script that checks remote server for a file, and if it is already available locally it should use that one. If it isn't available locally, then download the image to the server. However, I'm struggling with the script overwriting images that I already have locally. If it's already available locally, the script shouldn't do anything with the file - just display it.
In some cases, the script tries to download a image that I already have - and the filesize for the file it overwrites becomes 0kb even though the file worked perfectly earlier.
What am I doing wrong here?
<?php
$url = "http://www.myserver.com/image123.jpg";
$filename = basename($url);
if(!file_exists(images/$filename))
{
// File doesn't exsist, download it!
$image = file_get_contents("$url");
file_put_contents("images/$filename", $image);
$image = "images/$filename";
} else {
// We already got this file, display it!
$image = "images/$filename";
}
echo $image;
?>
<?php
$url = "http://www.myserver.com/image123.jpg";
$filename = basename($url);
$path = "images/$filename";
if( !file_exists($path) ) {
$image = file_get_contents($url);
file_put_contents($path, $image);
}
echo $path;
?>
Are there any way to check the file befor upload it, it have virus or not ??
My code upload is :-
function uploadfile()
{
$allowedExtensions = array("jpg","jpeg","gif","png","doc","pdf","ppt","zip","rar","xls","pptx","docx","xlsx");
$split=explode(".",$_FILES["filework"]["name"]);
$type=strtolower($split[sizeof($split)-1]);
$rname=time().".".$type;
if (in_array(end($split),$allowedExtensions)) {
$destination = "uploads/".$rname;
$temp_file = $_FILES['filework']['tmp_name'];
move_uploaded_file($temp_file,$destination);
return $rname;
}
else {return false;}
}
No, the file must be uploaded to your server before you can scan it. If you don't have the file yet, how can you scan it?
I have a script working to upload images without refreshing the page using jquery.form.js
Below is the PHP file that it kicks off the processes the file and when finished PHP creates an tag to show the image.
I now need a method to let JQUERY know the PHP file processing has completed. Is there a good way to connect these two?
I thought I could write something hidden to the page and have JQUERY look for this but I'm not sure if this is a B-Grade solution.
Any ideas? I can explain better if needed. thx
<?php
$type = $_POST['mimetype'];
$xhr = $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
foreach($_FILES as $file) {
$filename = $file['name'];
$filesize = $file['size'];
$filetmp = $file['tmp_name'];
}
// Script Variables
$errors=0;
$notAllowedFileType=0;
$exceededMaxFileSize=0;
$maxsize='10485760'; // 10MB Maximum Upload
$folder = 'images/';
$configWidth = 500;
$newFileName = 'success_story'.time().'.jpg';
// Process if No Errors
if(!$errors) {
// Variables
$uploadedFile = $filetmp;
$filename = basename( $filename);
$extension = strtolower(getExtension($filename));
// Convert the Specific Type of File into an Image
if($extension=='jpg' || $extension=='jpeg' ) {
$uploadedfile = $fullfilepath;
$src = imagecreatefromjpeg($uploadedFile);
}elseif($extension=='png') {
$uploadedfile = $fullfilepath;
$src = imagecreatefrompng($uploadedFile);
}else{
$uploadedfile = $fullfilepath;
$src = imagecreatefromgif($uploadedFile);
}
// Configure Width & Height
list($origWidth, $origHeight) = getimagesize($uploadedFile);
$configHeight = ($origHeight/$origWidth)* $configWidth;
// Create Empty File
$tmp = imagecreatetruecolor($configWidth, $configHeight);
imagecopyresampled($tmp, $src, 0,0,0,0,$configWidth,$configHeight,$origWidth,$origHeight);
imagejpeg($tmp, $_SERVER['DOCUMENT_ROOT'].$folder.$newFileName,90);
echo "<img src=".$folder.$newFileName." id=\"cropMeNowImage\">";
}
// Get Extension from Uploaded File
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) {return "";}
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script>
(function() {
$(document).on('change','img#cropMeNowImage', function() {
alert('Ready to Crop');
});
})();
</script>
You need to use a callback function. Something like:
$(document).on('change','img#cropMeNowImage', function() {
$.post(url, {
vars_to_server : vars
}, function(process_done){
if (process_done)
alert("ready");
})
});
php must echo something recognizable by jquery in the var process_done
Instead:
echo "<img src=".$folder.$newFileName." id=\"cropMeNowImage\">";
you can echo 1 for success
This is the idea. It's totally possible. Specially for the common task of upload files via AJAX...
I recommend you: 5 Ways to Make Ajax Calls with jQuery
Take a look to http://github.com/valums/file-uploader I always use it to upload files.
I'm using the Uploadify jQuery plugin for PHP to upload a file. One thing I am stuck on is that I need to be able to rename the file being uploaded so that I can post that information to my script that inserts data into the mysql database. Can anyone please advise on how to do this?
Thanks,
Jake
Firstly rename the filename as you want in your uploadify.php
Then you just need to return $targetPath from your uploadify.php file, like this --
echo $targetPath
no need to use echo '1'
And now you have to get the renamed file name. You can get this in onUploadSuccess function.
onUploadSuccess() takes three parameters (file, data, response),
where file gives you the actual name of file you browsed through your computer, and data gives you the renamed file name which you generated through your uploadify.php code as per your requirement.
So you can try the below code --
'onUploadSuccess' : function(file, data, response) {
alert('Renamed file name is - ' + data);
}
I hope this will help out. One of my friend told me this, and I got my work done then :)
you can do like this :-
$targetFolder = FCPATH.'/resources/images/users/temp/'; // Relative to the root
if (!empty($image))
{
$time=strtotime("now");
$image['Filedata']['name']=$time.'.jpg';
$tempFile = $image['Filedata']['tmp_name'];
$targetPath = $targetFolder.$image['Filedata']['name'];
// Validate the file type
$fileTypes = array('jpg','jpeg','gif','png'); // File extensions
$fileParts = pathinfo($image['Filedata']['name']);
if (in_array($fileParts['extension'],$fileTypes))
{
move_uploaded_file($tempFile,$targetPath);
echo '1';
}
else
{
echo 'Invalid file type.';
}
}
If you use uploadify.php just go right before the function move_uploaded_files and change the target name.
Anyways you do it this should work. Post the code you have if you want a more detailed answer.
This will put file in new folder with same file name as source file
$source = $_FILES['Filedata']['tmp_name'];
$filename = $_FILES['Filedata']['name'];
$newPath = $folder.'/'.$filename;
rename($source, $newPath);
/*----------------------*/
to have a new filename
function getExtension($path)
{
$result = substr(strtolower(strrchr($path, '.')), 1);
$result = preg_replace('/^([a-zA-Z]+)[^a-zA-Z].*/', '$1', $result);
if ($result === 'jpeg' || empty($result) === true) {
$result = 'jpg';
}
return $result;
}
$source = $_FILES['Filedata']['tmp_name'];
$filename = $_FILES['Filedata']['name'];
$newfileName=$folder."/"."abc".getExtension($filename);
rename($source, $newfileName);