I'm really enjoying learning php and I've come a long way in a couple of weeks but I'm sorely stumped on something
and hoping for some help / guidance / lessons !
My user will upload a picture (to a dir not a DB) and that picture will display in their private gallery.
It will also display in a public gallery but I don't want two copies of the img on the server.
So I came up with a groovy solution of having the upload script write a path like members/1/images/1.jpg to the uploaded image
to a txt file which can be read by the front end to show the images in the public gallery.
So far so simple. It works.
The front end just reads the txt file which holds a string with all the paths to all the user uploaded images.
members/1/images/6.jpg,members/99/images/123.jpg etc
My problem is related to updating this txt file when a user uploads a new image.
So...
The txt file holds a simple string like this allpics=members/1/images/1.jpg,members/99/images/60.jpg,members/25/images/3.jpg&totalimgs=3
The "allpics" and "&totalimgs=3" are required variables that will be read by another app. (which speaks another language).
At the end of the image upload script I need to update (append) this string with
1) a new image path which is held in $stringData
and
2) the total number of image paths in the string i.e. &totalimgs=99 (or whatever number).
So I tried to write a script that would:
open the txt file.
count the commas in the string
strip out the last 13 characters i.e. "&totalimgs=99"
append the new image path ($stringData)
then append the string with "&totalimgs=" plus the number of unique image paths (i.e. the number of commas or
the number of occurrences of "jpg" in the string).
But I just can't make it work !
The code I'm using (and failing with) is below and if anyone wants to have a go at fixing this I'd be mightily grateful !
Best wishes
Monty
Code: Select all
$place_file = move_uploaded_file( $_FILES['fileField']['tmp_name'], "members/$id/images/".$newname);
chmod ("members/$id/images/$newname", 0666);
// NOW OPEN THE .txt FILE IN THE APPROPRIATE GALLERY FOLDER
$myFile = "images/gallery/".$_POST ['gowhere']. "/gallarray.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
// READ THE CONTENTS OF THE FILE INTO A STRING
$newstring = file_get_contents($myFile);
// STRIP THE LAST 13 CHARACTERS (&totalimgs=)
$stripper = substr($newstring, 0, -13);
//print $stripper;
//APPEND THE FILE WITH THE NEW IMAGE PATH
$stringData = "members/".$id."/images/".$newname .",";
fwrite($fh, $stringData);
// COUNT THE COMMAS (the number of unique image paths)
$haha = substr_count($stripper, ',');
// APPEND THE FILE WITH THE TOTAL AS A VARIABLE FOR FLASH !
$stringdata2 = "&totalimgs=" .$haha;
fwrite($fh, $stringdata2);
fclose($fh);

