Do I need a mysql database to upload images used temporarily? - php

On my website I would like to have the possibility of uploading an image, modifying it and printing. I won't be using the images later on, it's important for me to have them available until leaving the "print" page.
Do I need to create a database in order to store the images or is there any other (more simple) way of achieving this? Can you store such things in RAM (excuse my noob question)?
Basically I need to upload the image, display it on the screen, play with it and print it.

Alternativly:
When a user enters the page and uploads an image, create a new folder for this user only and store the relative path in a cookie. This way you could grab the users folder at any time no matter what the user is doing (leaving, coming back, reloading etc.).
Appended according to the comments
Dont use plain-text values (I bet nobody knew this when using cookies).

Well, I would upload the image to the folder on a server (user-images), then save the image-path as a $_COOKIE variable (so you can pass it to other files like modify.php or print.php). You'll delete the file with the unlink() function when the session data is no longer available. Something like:
if( empty($_SESSION) ){
unlink( $_COOKIE['img_path'] );
setcookie( "img_path", true, time()-10 ); // set negative expire time in order to destroy the cookie
}

Related

Secure files and display them as well

My question is about HTML and PHP.
This is my setup right now:
A website where user have accounts
A FTP server with pictures (currently none)
Files are currently saved on the website in the "PICTURES" folder (which is accessible by everybody who know the full URL)
So, I would like to know how I can display the images without storing them on the website (which will fix my URL problem).
My idea was to move the files on the FTP server, and when a users logon and request a page with those images, download them through a FTP connection, save them on the website, display the images, and remove them. Which would make them accessible only between the downloading time. But this solutions sounds REALLY bad to me.
You need always to have a place where your images are stored. But, if you don't want to give a user the chance to know where are stored, you can create a system which is used to show the images.
Think about this, if you want to download a file from Mega, you can't access to the URL where the file is stored, instead of that, the server itselfs calls a system who assign you a "key" and you can download the file only through that system using your "key".
You could use a system like "base64" so you can encode your image, and show it using it, or, you can use the "header" modifier so, you can display an image using a PHP code.
For example your image tag will be like:
<img src="processImage.php?id=01&user=10&key=123" />
So, your processImage will return a "tricky" image, actually not the image, but the code processed by PHP will be returned, like using "imagejpg()" function with the header "Content-Type:image/jpeg" and then the user will not know where the image is stored actually but the img will works actually.

CrossBrowser: File Upload Issue (HTML+PHP) - NOT TO UPLOAD IMMEDIATELY

Is there a possibility to NOT upload a file IMMEDIATELY, but to store maybe in an array to upload it in a later step?
I want to ensure is that the user can navigate freely between the form pages and then upload the files in the last step. The only way I know is to encode the files and store them in the session, but this is anything other than elegant.
It should also be a cross-browser solution.
If you have a form splitted over different urls and you have a file input in a previous step no, it isn't possible.
unless...
if your "cross browser" requirements can ignore IE<10 and every non recent other browser, you may use the javascript File Api to read the file client side, eventually store it on the client with sessionStorage/localStorage and send it to you later
See
http://caniuse.com/#feat=fileapi
https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications
https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage
When ever the user uploaded the file then that file will be moved to the temporary location.
That temporary file will be deleted when ever the script is finishes.
In your scenario if the user moves from one page to another page then the file will be deleted automatically

Browser temporary cache

I am creating a dynamic image dependent on a lot of information about the logged in user, but with so many users online, the image system is using a lot of the resources my website hosts have allocated to me.
Is there a way to tell the browser to temporarily cache an image? Similar to temporary cookies?
The image contains numbers such as post count and other frequently incrementing numbers that can cause the browser's automatically cached images to re-download the image potentially every page load. I wouldn't mind a little inaccuracy to save a lot of processing for my server
[Edit]
Browsers already do cache content(unless specifically told not to do so), if it detects a change in last modified then it reloads it.
I am taking a guess that your image being generated is either a) being returned to the browser by a specific php script() or b) is being stored temporarily but is being updated and so the browser retrieves the updated version.
[/Edit]
If you are outputting an image based on dynamically generated content you can write this temporarily to a directory and then based on a predefined time period pull the image if its valid.
if(file_exists($file) && filemtime($file) <= time()-60*20) {
// Regenerate image
} else {
// Load image from cache
}
The example above will not regenerate the image for 20 minutes.
Try to look here
http://www.webscalingblog.com/performance/caching-http-headers-cache-control-max-age.html
In other way, you can set up cache-control header in PHP, and display images using script.

PHP user profile picture cache

Okay, so on my website a user can upload a profile picture. But the issue is, if they update it by uploading and overwriting the existing profile picture they have to wait for their browser cache to clear and the same for everyone else on the site.
I know I could easily beat this by sticking a string on the end of the image URL e.g. ?id=22185 , but that will make my site loading times VERY slow.
Could any of you recommend a way of making the user's profile picture update instantly for every user on the site?
Use the file modified time as the URL variable. That way the image will be cached until that number changes, which only would happen of the file is updated.
Set unique name for each image. When user change image, e filename change too and browser will load new image instead of serving old FROM cache
You could easily add a timestamp to your files or you could use the "headers" function of PHP to change the "Expire" param.

How to pass input type file (i.e. image) directory as session

I have a multi-step form in which i have to choose an image in step 3 and have to pass the image directory or path as a session in another input field in step 4 before submitting form.How I can pass the image path to finally keep it in database??Please Help !( PHP & Mysql)
You can't. There is no way to know where on the user's hard drive a file is stored, let alone a way to go and get it from the web browser (imagine if I made a website that went after your windows directory files; even your password files -- there are obvious security reasons why web browsers don't allow this).
What you can do is upload the image, then use info stored in $_FILES to temporarily track where the file is located on the server, and at final submission do whatever you need to do with it.

Categories