PHP: Execute another PHP file without including it - php

When a user uploads an image on my website, I want to automatically create a smaller thumbnail for it and save it - without displaying the thumbnail to the user after the upload is done.
I'm doing the thumbnail creation in a PHP file with an Image-Header and I'm currently executing this PHP file by including it in the HTML response after the upload is done:
<img src="createthumb.php?id=ID_FOR_JUST_UPLOADED_FILE" style="display: none">
That seems to be a very hacky way for me to execute the createthumb.php file and it also seems to fail sometimes - for example when the user leaves the page before he retrieves the HMTL response after the upload; or maybe because some browsers don't load images with display: none.
Either way, I'm trying to find a PHP function which executes the createthumb.php (asynchrounously) without including it in the response to the user.
include and require obviously don't work.

It sounds to me like you want AJAX.
The basic concept is that your client side script (maybe JavaScript) can generate a request for your server script (php in this case). The server script receives the request and processes it, then sends an asynchronous return message to your client script.
For your case, you could have a JavaScript function triggered on any file upload. The function sends your request to the php script and creates your thumbnail in the background without disturbing the user's current view. Take a look at the W3Schools tutorial.
http://www.w3schools.com/ajax/

Related

file upload using angularjs in php on button click

the upload using the angular frontend (whichever way this is handled) sends the file data to a script on the server such as a php script (which is my preferred method). Once the php script has run I want to return to the page from which the upload was made and give a message there..I dont want the php page to display. Will appreciate some guidance on how to achieve this. Ideally what code to add to the php script. but i want upload procedure on button click or ng-click not "onFileSelect"
You can use ng-file-upload directive, with many server side samples.
Follow here: https://github.com/danialfarid/ng-file-upload/wiki/PHP-Example

Serving Images to get a request

I am using CodeIgniter rest Controller. I want to be able to serve images on GET requests made by the client.
Is this the best option or should i just provide a link to the image and let the client download it themselves?
If I can serve the images itself, than how should I?
To make CodeIgniter set the proper headers and such you could do this in your controller:
$this->output->set_content_type('jpeg')->set_output(file_get_contents('path_to_file'));
And it will output (as request response) the image content as a file. No view required.
Please note though that this is a bit of extra overhead as the file is processed by PHP instead of just the webserver (Apache/Nginx). It only makes sense if you need to have some business logic on the request, such as logging or authorization (though even that can be done without PHP). If you are just outputing the image it is therefor better to link straight to file and leave PHP out of it.
I think You could show the images to client and give them a check-boxes to download the images as a Zip file.
See this

How to write to database in Symfony2, when a link is clicked on the website?

In a project I'm working on, the client has required that every time a user clicks on a link to download (they will be downloading a video or mp3), there should be a log kept of who has downloaded what and when.
I have a table in my database set up to record the User ID, the File ID, and the date when it was downloaded. However, I don't know how to do this, as the link is basically an tag (obviously).
What is the best way to achieve this?
Probably the simplest solution would be to write simple onclick ajax event.
If you want noscript solution you'll have to create some download wrapper, that'd serve you proper file. Just create special route and controller (eg. /downloads/filename), increment download meter for this one and return asset instead of html response. Don't forget to set proper Content-Type header tho.
There's also IgorwFileServeBundle that could help you loads.
Instead of linking to the MP3 file, you'll have to funnel the download through a PHP script that writes to the database and then sends the MP3 data with with the right headers. For maximum performance use the "X-Sendfile" header instead of the PHP readfile function.
Alternatively you could set up a cron job for a Symfony console command line tool that parses the Apache access log and writes to the DB whenever it encounters an MP3 file.

Submitting a file via AJAX

Started with ajax last week. How would I upload an image using it. What I want to do is upload an image. I would imagine using input type="file" then the PHP file needs to collect the URL of the newly uploaded image. And return it to the ajax function to put the URL inside an <img src"" >
How would one do that.
Any ideas?
Marvellous
I would recommend Valums ajax upload script. If it comes across a browser that does not support upload via ajax it will gracefully handle them via hidden iframe uploads
Note: IE (all flavors) does not support file uploads via ajax and will therefore default back to iframe upload.
You can't send files through AJAX consistently. You will need to submit your form into a hidden iframe to simulate an AJAX transaction. Using an iframe ensures that your application will be compatible with all browsers - particularly the IEs.
After digging through my bookmarks a simple script to facilitate this might be found with http://valums.com/ajax-upload/ but I have not used it.

Triggering a FLASH Event function

I am trying to build a simple application. The first phase is selecting to upload a file from the user desktop or from the site's gallery.
The question is if the user chose to upload from the gallery, say, go to an PHP page gallery, choose a photo and then redirected to the Flash app. How would you trigger the Flash up to know there was an image chosen and it should load it?
Does PHP send a variable or create an XML based on a choice and pass it via Flash variable, or does it need JavaScript to tell Flash that if the Flash var is not empty run this function? Could you please give a sample script I could work on?
Why are you starting the upload process outside of Flash when you want to use the file inside of the Flash application? Use FileReference to initiate the file upload, save the image name locally and have the php script you hand the file off to store the file in a known place, then listen for the Complete event. From there, you should be able to load the file into Flash from your server using the path "KnownDirectory/SavedFileName" with URLLoader.

Categories