By mike@mike-miles.com
Recently for a project I needed to build a custom photo gallery web application, which would allow the administrators and site users to both uploaded photos. The thing about file uploads is that to the user (which in my case was administrators and site visitors) is that it can be very boring to the user, and not very elegant. Since users were providing additional information to the photo, I was saving everything to a database including the filename of the photo uploaded (for each upload I would generate a random name). I wanted the user to be able to upload their photo, preview it, then finish completing the rest of the form. Normally to do this would require at least 3 steps;
- Have the user upload their file, and submit the page.
- Reload the page, save the file then display a second form for the user to provide details
- save the details to the database.
Now this fine, a tried and true way to do it, but boring to the user because before they can finish filling out the form they would have to wait for the file upload to finish (which could take awhile depending on file size and connection). We dont want the user to have to sit there for a long time waiting for the upload to complete (idle users are BAD, for a number of reasons, example it gives them time to say ’screw it’ and navigate away from our site). It would be much better if the user had something to do while their photo was uploading, keeping them busy and happy.
So what I wanted to do was have the user select a photo, which would then (using AJAX) be sent to another page to upload, and return the file name. While this was happening the user would see a loading icon, and be able to complete the rest of the form. Once the upload page returned the filename, I would display the form’s submit button.
At first I was stuck on how to exactly accomplish this effect (I knew I wanted to use AJAX). After doing some googling I came across this solution.
Basically a full blown AJAX file upload is not possible, but we can get the feel and effect using a hidden Iframe.
Sorry to say I’ve had to remove this code for legal reasons.
but feel free to checkout the source I used to create the code to draft your own solution.
By mike@mike-miles.com
When using AJAX or sending vaiables to a PHP page to run a function there is a nice peice of code I like to put at the top of the PHP page. It is a peice of code that I use to run a function by passing its name through a variable. This allows you to GET or POST to a page many times and preform multiple functions. Here is the code and I’ll explain a bit more of a few ways that you can use it;
1
2
3
4
5
6
7
8
9
10
11
| <?PHP
//if passed the variable 'function' through GET or POST
if($function = $_REQUEST['function']){
//if function exists, run function
if(function_exitst($function)) $function();
//else show error
else echo 'Error: function '',$function,'()' does not exist.';
//Exit script
exit;
}
?> |
So to reiterate this is what this function does.
If the page is passed the varaible ‘function’ (either through GET or POST, hence the $_REQUEST) then it knows to run a function on the page.
It’ll then set the passed variable to the $function variable, and test to see if the function exists (by using PHP’s function_exists() function)
If the function does exists, the code then calls if ($function();), else it’ll show an error.
for example you could use this code like so;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| <?PHP
//if passed the variable 'function' through GET or POST
if($function = $_REQUEST['function']){
if(function_exitst($function)) $function();
else echo 'Error: function '',$function,'()' does not exist.';
exit;
}
function submitForm(){
$value = $_POST['field1'];
echo 'You inputed ',$value,'.';
}
?>
<html>
<head>
<title>Sample</title>
</head>
<body>
<form action="<?= $_SERVER['PHP_SELF']; ?>?function='submitForm'" method="POST">
<input name="field1" type="text" />
<input type="submit" value="submit" />
</form>
</body>
</html> |
So the form will submit to itself, and run the function ’submitForm’ when it does so. Of course this is a very simple example.
you can download the complete version of this code, on my code page
By mike@mike-miles.com
There is this nifty function in PHP called fseek(). It allows you to set a pointer to any where in a file (a text file for example). It comes in handy when your dealing with very large files (like log files, or config files), and today I discoverd it can be very useful if you want to search throu a file backwards.
Which was exactly what I was trying to do. I needed to traverse a file, starting at the end. The only solutions I could find infolved reading the whole file into an array, reversing the array and thensearching that way, which completly nullified the point of going the end of the file (I had to read the whole file into memory to get the end).
The solution I came up with, involves using fseek() and fread() (which allows you to read a file byte by byte). With this function you give it a file, and a string to search for, and it’ll go through the file backwards byte by byte, and return the pointer (byte location in the file) of where the search string occures. (if it doesnt find the string, it’ll return FALSE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
| <?PHP
function searchFromEnd($filename,$search){
//first make sure that the file is there, and we can read it
if(file_exists($filename) && is_readable($filename)){
//open the file for reading
$fp = fopen($filename,'r');
//get the length of the search string(minus the first character)
$size=strlen($search)-1;
//get first element in string
$first=$search[0];
//size of the file
$file_size=filesize($filename);
//set seek pointer to end of the file - size of the string
$seek=$file_size-$size;
//set found to FALSE
$found=false;
//while not past the begining of the file
while($seek>=0){
//set file pointer to seek value
fseek($fp,$seek);
//read in the first byte from pointer
$test=fread($fp,1);
//if byte is same as the first string character, and back far enough in file for search length
if($test==$first && (($seek+$size) <= $file_size) ){
//append enough characters to $test to make it same size as $search
$test.=fread($fp,$size);
//if test value is what we're looking for
if(strcmp($test,$search)==0){
//return position;
$found=$seek;
//set pointer to -1 (exit loop);
$seek=-1;
}else{
//move back a byte
$seek--;
}
}else{
//move back a byte
$seek--;
}
}
//close the file
fclose($fp);
}
//return FALSE or position of search string
return $found;
}
?> |
A much better method then reading the whole file.
You can download this file from here