PHP: Check if there's an active PHP upload - javascript

Is it possible to check if there's an active file upload via PHP?
I guess this would have to be done using JS if there was any way at all but I'm unsure of where to start. From Googling, I think ignore_user_abort() may be the function I need as that way I wouldn't have to worry about any checks, but it would be interesting to find out if JS can detect an active PHP upload

PHP is a server side language so once the php code is executed there is either no new file uploaded or there is a new file uploaded, it's never executed during a file upload because the file upload is attached to the request to run the php.
Unless you handle the execution of the submit form by javascript there is no way to track the uploading of a file because the moment you press submit in a normal html field the javascript on the page stops executing because the browser is working on the new page that is being loaded.

Related

Is it possible to upload a file on a secondary page?

I have a form with a file input which accepts large files. The intention is to upload this file to a remote server.
<input type="file" id="userfile"></input>
When a user chooses a [potentially large] file, I want to redirect to a secondary page (on the same domain) which shows the upload progress along with other information.
I can use the native File API to grab the Blob and read the file with FileReader, but only on the page with the form (the first page). I want to delay this reading/uploading process until the second page.
Is there a way to POST the file reference somehow to the second page, and then upload the file from there? Or is this impossible by design for security reasons? Or, some way to store the file reference in a cookie or local storage to be read by the second page?
I could double POST - first to the secondary page, and then POST/PUT to the appropriate final destination. However, I want to avoid this redundancy, which would cause further delays for the user.
Is there a way to POST the file reference somehow to the second page, and then upload the file from there?
No.
To get this sort of effect, the usual way is to remove the second page from the process entirely and use Ajax for any communication you need to do with the server before uploading the file to the correct place.

Javascript: Save uploaded file

I would like to save uploaded file using javascript, in my linux server. The code I wrote is:
if (uploadInput.files.length == 0) {
console.log("No file is uploaded. ");
} else {
console.log("File uploaded.");
var file = uploadInput.files[0];
}
Now I would like to save that file as "files/upload.csv". Can anyone please advise, how can I do it?
Thanks in advance
What I'm going to do is walk you through the logic, instead of providing code. There is just not enough information here on what you want to do to provide actual code and the sample you provided is a very small part of what the actual solution would need to include.
I'm assuming the code you wrote above is meant to run on a website visitor's browser (client-side). Client-side code can't save to a server. What it can do, is send the file contents to the server. But then you'd need something on the server side to process that file contents and actually save it to the server-side files directory.
One method to send the file contents from the client to the server is to use AJAX - you can do this with native javascript, but I would recommend looking into a library such as Jquery, which makes it a lot easier. See http://api.jquery.com/jquery.ajax/ This AJAX code will need a communication point on the server to send the file contents to. From your profile it seems you're familiar with PHP. You could make a php file on the server (say receivefilecontents.php) that takes in input from that client-side AJAX call, and then saves it to a server directory - you could also do this in Python, Java or a number of other languages.

Escape <?php ... ?> on javascript file

I'm busy doing server side and client side validation for magento. this validation works fine on server side (php)
on the client side i am using javasrcript.
When i started on this. i had my javascript embedded on a phtml file and everything was working as expected.
because i am using magento so I decided to inject the javascript file via page.xml
When I added the javascript code instead of getting the message pulled I get the php as is.
Here is my javascript:
function DefaultAddressErrorChangeNotAllowedMessage() {
alert("<?php echo Mage::helper('invent_general')->getDefaultAddressErrorChangeNotAllowedMessage();?>");
return;
}
I run this when a user hit the onclick it will point to this function DefaultAddressErrorChangeNotAllowedMessage()
and the
<?php echo Mage::helper('invent_general')->getDefaultAddressErrorChangeNotAllowedMessage();?>
will be populated as is.
but when I embed this directly to a phtml file it pull the correct message.
I there a way for javasrcipt that I can use to escape the php and get the correct message which is pulled from config.xml
PHP is rendered server side only. If you need to "inject" PHP specific values to your javascript, then you either need to render the actual value as part of the output of the php script, or you need to take a new roundtrip to the server, using Ajax.
Javascript is clientside, PHP is server side, so all php has been evaluated when javascript is loaded. This means, you can alert php echos, but you can't run PHP operations or any PHP logic in Javascript. You need ajax for this.
sorry for my clumsy answer, but maybe you lost the simple things.
I see that your javascript contains php tag, so i think you should insert your javascript code into .php extension because .js extension can't recognise the php tag.

How to pass an uploaded image back to html

The setup I work on is the following: A HTML-page with a canvas element displaying a Processing JS sketch and a form that allows to pass parameters to the sketch using JS-function Processing.getInstanceById(getProcessingSketchId()).setSomeParam();, which works just fine.
The user should also be able to upload an image to the server, the name/path of which is then also handed to the sketch to do loadImage(String path). The file upload (using PHP from here) works perfectly fine as well but now here is the question:
How can I get the name of the uploaded image on the server and pass it to the Processing sketch?
If the form's action tag points to a distinct PHP-file, the site with the canvas sketch is quit (variable $destination not available) or if the form's action tag points to the site itself (being a PHP-site, checking for if(isset($_POST['formSubmitted'])) {), the sketch is restarted losing all the previously changed parameters.
I also managed to have the form submission target to an internal iframe and display the uploaded image there, but that still doesn't solve my problem.
Thanks a lot in advance for any helpful hints!
It sounds like you have the data flow worked out but are struggling because a standard form upload causes a page refresh.
I would suggest using AJAX to post the image to a specific PHP image receiver service. When the AJAX upload is complete, the javascript can then call to the server to fetch the uploaded object and manipulate it as needed without refreshing the page.

Rechecking a file for updated content

I'm trying to recheck a file on a timed interval. I'm not sure if this can be accomplished, but any help would be greatly appreciated. I understand that as PHP is server-sided, it cannot accomplish my task, so anything with JavaScript is fine.
By rechecking a file, I mean getting a file's contents, but only if it's updated. Somewhat like "if file gets new contents, output them."
If you are checking on a remote file, then the PHP solution would be best: simply send the file and its checksum, then when the checksum changes, the file has been updated.
If you're working with a local file, then (if I'm seeing things right) you'll need a browser with the HTML5 File API. May be a duplicate of this: Check if file has changed using HTML5 File API

Categories

Resources