Where should I put all these uploaded files? - php

I'm building a webapp that as a small subset of one of the features allows images to be uploaded. I'm running a lamp stack, with Mongo instead of MySql.
I use a javascript uploader with a php backend to upload the files. The whole framework is under version control though, so I don't want to dump these files anywhere inside my framework, as it would get messy with the version control, and sub-optimal when I eventually migrate the media over to a CDN.
So, my question is - On a VPS, where should I drop these images for now? In some folder external to my framework? In my DB as bson? I've heard Mongo does a decent job handling binary data...
And, as a follow up, if I'm eventually planning on moving the content over to a CDN, how would you recommend structuring my schema for now?
My current plan would be something like the following:
All uploads are named with a unique
ID and dropped in an external
folder, defined by a globally
accessible variable of sorts.
A reference to each images' name is
stored in the db.
Is there anything obviously stupid about going about it that way, possibly causing me a scaling headache later?
Here's a summarized specific question, just so this is a little more of an SO friendly question:
Given a Linux, Apache, Mongo, PHP Framework on a VPS, what is the best way to store uploaded images while keeping scalability and speed as the 2 most important factors in deciding on the solution?

if your plan is moving to CDN, the answer couldn't be more easy: create a subdomain on your VPS, and drop your images there, and you will have decent CDN simulation as well as reliable file storage.

I do agree that you should never put user uploaded content inside your webapp for many number of reasons but then one of the problem is how to show img tag in HTML which takes src attribute relative to webapp.
An easy workaround can be: Suppose create a folder /usr/ImagesUPloadByUser on your unix box and drop all images there. and then create a link ( linux link) in your webapp which points to correct directory. Now the images are not residing in ur webapp and you also have easy access .

Related

File and Folder Attributes - Programming API

I knew that PHP is able to read file content by different ways, for example: fread, file_get_contents, file, readfile, etc.
Currently, I am looking for an API that can read real index of files and folders in specific partition or folder, for example:
drive d:\ in windows contains three folders (folder1, folder2, folder3), and each folder contains some files, we can get these directory structure using PHP (opendir, scandir, readdir, etc) and list them as I want, however, windows saved file and folder names inside hard-disk with their attributes (size, last modified, created on, etc).
How I can read hard-disc using PHP and retrieving all file and folder attributes for a specific path?
for instance, if we consider last modified time we can use (filemtime()) function, but this attribute not saved inside the file, its saved some where else inside hard-drive, other attributes also saved in other location not inside the file.
When windows user copying file from flash-drive to local hard, windows will copy all file and folder attributes and saves them inside local hard drive. When using PHP for copying file, it depends on OS to handle this job, its not native support (as I think) for file and folder operations.
Do you have any idea?
There are many recovery program that uses this technology for reading hard-drive indexes, however, for PHP: I cant find any source for this problem.
Applications if I get correct answer:
I can check if such file securely deleted from my hard-drive? I can create secure delete application using PHP, or clearing hard-drive indexes for a given file.
Your help appreciated.
Problems with the proposition
The attributes of files, such as timestamps, permission flags etc, are stored in the file system (FAT, NTFS, Ext3 etc). As you say some of them can be read using PHPs different file and directory methods, but they all act through the OS file system abstraction and cant have access to block level information on the disk, such as what precise byte on disk stores the archive flag for file X. The whole point of the OS and FS is to abstract away this information from the user/client programs.
As suggested there are external tools, written in c or similar, that does have this access and that you can call from inside PHP. If you want a 'native' PHP way of doing this you'll have to compile a c extension for PHP that exposes these low level functions to you.
I'd say external tools is the way to go if you want to stick with PHP but for the task at hand, as far as we can see from your description, I'd go with another language that has more low level access. Like C or C++. PHP is a high level language for HTML pre processing and as such is a poor choice for low level system programming.
Practical advice
After looking through the PHP documentation and assorted third party libraries:
An of the shelf solution for reading file system information on a file allocation table level doesn't exist for PHP. The lowest level you get is the fstat() function, and that is not very far for what you want.
External tools
No mater exactly what you want to do there is probably a small binary that does it. PHP can be integrated with these programs, as suggested elsewhere, via the exec() function. This is probably the easiest approach for you unless you have serious amounts of time and/or development resources to devote to this problem.
Wrapping a library
There are libraries that solves this problem for you, written in low level languages. An open source library can be wrapped with SWIG to expose it to PHP. This will give you access to the low level methods you need, but it's a non trivial task. These kind of libraries also often require sole access to the device while they work on it, something that is difficult to achieve in most normal operating environments.
Note also that you will probably need a library per file system. Microsofts VFAT extension to FAT12/16/32 requiers a licens to use. So if you want to work with FAT and have files with long names (not 8.3 format) you'll have to fork up some dough to be legit.
Low level implementation
A last middle ground would be to write your own CLI tool that uses an external library to access the low level FS functions. You can then use exec() from inside PHP to interact with your own implementation.
This might be a reasonable path if you cant find an existing tool that solves your problem and you are not willing to spend the time to wrap a library.
In closing
You give a very narrow problem description with little to go on as for what the application is about. A broader discussion (in another forum) might yield better results since the problem might be better solved in another way entirely.
I found something on PHP.net which appears to do what you want:
http://php.net/manual/en/function.readdir.php#103418
Edit: I mis-understood the question. Attributes such as the last modified time, last accessed date and the like are stored in the file systems master file table. As far as I can tell, this isn't accessible with PHP, and if you were to write your own method to do this then you'd also have to account for different file systems as they all handle the storage of these attributes in their own unique way.
It could be that to get all of the information you're looking for is not possible with PHP without writing some form of extension to PHP itself.
Edit 2: Upon researching a little more...
http://php.net/manual/en/function.fileinode.php
This function could be an interesting one to look at.
Well if I understand correctly you just want to securely delete a file. You can just call [shred][1]
[1]: http://linux.die.net/man/1/shred via system or exec if you are on linux and you are good to go

Creating an image from an HTML document at runtime

I am currently working on a website project that requires the creation of an image based on user input on a form. Basically, I'm trying to create an image from existing HTML markup with the form data replacing some of text. The text is almost always unique.
I have explored several options for creating such an image, mainly:
imagecreate with PHP that wasn't flexible enough, and
PhantomJS which I can't really install on the server
wkhtmltopdf and php-wkhtmltox
I am working in a shared-hosting environment that limit my available options. The environment supports PHP (compiled with GD), Perl, and Python.
Is there a reliable way to implement such a behavior?
I use CutyCapt software to render images from html, it works perfectly. The only issue is that you need to build it on your shared hosting. Probably the workaround could be in building it on your local machine, uploading it to your hosting and running it from there, but I don't believe it will work, because it requires X server.
Another possible solution could be use of GD library to build the image manually. If you know how your html is looks like, you can replicate it by drawing all elements on a blank image. It is not easy to do, but looks like this is only one solution which you can use.

Cross Browser Cross OS File Dialog (with ability to select directory only as well as files) Non Java

So I am looking for a method of opening a browsers file dialog, and allowing users to either select a directory and have it populate the path, or select a file or set of (within the same directory). The isn't a Java Applet. Is there a means of this type of dialog? That can support IE 7 and up? Or am I stuck with a Java solution?
We currently have a Java Based solution that was written by someone years ago, which is starting to fail more and more due to new versions of Java coming out, due to OS's getting more tighter on security, and so on and so forth. So I am trying to find alternative solutions that will hopefully be a bit more easy to maintain, that don't require (preferably) java to do it.
I am ultimately just stuck with where to look to even begin to start finding any of the right methods to do this if its possible in the first place. Seems every search I do, just brings me in circles with answers that dont apply
EDIT
I should put emphasis, that the only goal of this dialog is so I can get a directory and or file path from the user on their local machine, without having the user key in the full path themselves.
As far as my knowledge go, it's only Java or possibly some AIR+Flash hybrid in this case, really. Reason for this being that you will need some technology to:
recognize your selection as file or directory
read all files and possibly sub-folders from the selected directory
queue them for upload and then be possibly able to recover their structure on the server-side
Therefore you at least need something that can natively work with file structure of the client, as well as optionally something that will re-create the same structure on your server.
EDIT: you may also want to check this: Upload a folder in ASP.NET of Flash or Silverlight

Website doesn't work during uploading of crucial files

I have a problem with maintenance of my php based website. My website is built on the Zend Framework. When I wish to upload a new copy or version online - during the time of upload especially when crucial files like models and controllers are being uploaded and rewritten - the site won't run understandably.
Is there a way to upload a website without having to go through this issue?
My updates are really quite regular. Let's say like once or twice a week in this case.
You can make use of the fact that renaming directories is quick and easy even through FTP. What I usually do is:
Have two directories, website_live and website_upload
website_live contains the live website (obviously)
Upload contents to website_upload
Rename website_live to website_old (or whatever)
Rename website_upload to website_live
done! Downtime less than two seconds if you rename quickly.
It gets a bit more complex if you have uploaded content in the old version (e.g. from a CMS) that you need to transfer to the new one. It's cumbersome to do manually every time, but basically, it's just simple rename operations too (renaming works effortlessly in FTP as well).
This is a task that can be automated quite nicely using a simple deployment script. If you're on Linux, setting up a shell script for this is easy. On Windows, a very nice tool I've worked with to do automated FTP synchronizing, renaming and error handling - even with non-technical people starting the process - is ScriptFTP. It comes with a good scripting language, and good documentation. It's not free, though.
If you're looking to get into hard-core automated PHP deployment, I've been doing some research in that field recently. Maybe the answers to my recent bounty question can give you inspiration.

One code, many websites

I need to develop a project that would allow me to instance many copies of a website, but each copy needs to be a separate website. I could upload the same code to many different accounts, but I would prefer to have only one copy of the code. Each website would be an "instance", so to speak. This way I could upload the code once and update all the websites at the same time.
For technical reasons I need to use PHP (but I'm interested in the other options too, for my own knowledge), and I thought Jelix could be a good choice of framework. Are there better options out there?
You can have all code in one directory, and then create virtual subdirectories in all your web sites, which all point to this directory. This is how Microsoft solves the problem in SharePoint.
The easiest bet is to have all the websites link to one server (perhaps distributed).
Pass the calling URL through your webserver to generate configuration information. Use those passed URLs to define the differences between each site.
Beyond that, the framework is almost immaterial to the question, so I'll leave it to someone else to answer.
Just remember, if you make 20 copies of the same code, that's 20x the time it'll take to fix bugs.
If you're using UNIX or Linux for a web server, you could create one master copy of the PHP code, and then use symbolic links to the actual files that are in separate directories with virtual websites set up in Apache. You could also put site-specific config files under those directories, but the bulk of the PHP code would be resolved as symbolic links to the "master" code.
I'm not sure what kind of websites you're talking about, but why not use an already developed application like Wordpress or any other cms? The code is identical on every website, and you can easily update it. The website-specific data is only present in the single configuration file, and the MySQL database.

Categories