It's a string thing....

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

It's a string thing....

Post by MiniMonty »

Hi all,
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);
 
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: It's a string thing....

Post by califdon »

Are you just doing this to learn how to handle text files? Because this is where you should be using a database. If you don't need the power of MySQL, you could use SQLite, but as a practical matter, using a text file is undoubtedly the hardest way to do what you described.
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Re: It's a string thing....

Post by MiniMonty »

califdon wrote:Are you just doing this to learn how to handle text files? Because this is where you should be using a database. If you don't need the power of MySQL, you could use SQLite, but as a practical matter, using a text file is undoubtedly the hardest way to do what you described.

I know...
I'm just a bit new to the whole sql / php thing and I've been importing data into flash the same old way for so long...
So old habits die hard.
But must die soon I guess.
If I could find a good tutorial (or tutor...) to deal with this via a db I would do it because I can see the potential power therein !
But I know nothing of it's buttons, knobs and levers.
It's all a bit Wizard of Oz to me and I'm scared of it !

Care to take me on a step by step course in image management vis an sql db ?

Coz I'd luv ya if ya did !

Best wishes
Monty
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: It's a string thing....

Post by Weiry »

i agree with califdon, you need to start using databases :)

Do you use WAMP at all?
Because WAMP comes with apache, mysql and php preinstalled. And also comes with a very nifty application called "phpMyAdmin" which is used for managing MySQL databases.
WAMP

If you already have that installed, then here is a great video on how to use phpMyAdmin:
phpMyAdmin Tutorial - The Basics

Once you start getting the hang of managing databases, you can follow this tutorial to get stuff out of the database:
W3Schools - PHP MySQL Introduction

With a bit of modification of the above tutorials, im sure you'd get the hang of managing your images :D

NOTE: With SQL you can store images in the database itself as a GLOB, but this is more advanced, so you would be better off sticking with your original idea of storing the path to the images. Then working up to that later.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: It's a string thing....

Post by califdon »

Weiry gave you several good links to get started. As he said, most developers store the paths to images in the database, rather than the images themselves, for several reasons. But the main thing is that inputting and retrieving data from a database is worlds easier than struggling with parsing a text file.

For something as simple as a bunch of images and associated owners, a very simple table would probably give you everything you need, certainly as much as a text file. A table always represents some entity--persons, places, objects, events, documents, transactions. Each instance of such an entity is represented by one row in the table. An entity has attributes--for a person, it might be first name, last name, address, birthdate, etc., for a transaction, such as a purchase order, it might be PO number, customer, item ordered, price, quantity, etc. Every row in a table has the same attributes (or "fields"). Unlike a spreadsheet, a database table is strictly organized--no extra rows with headings, for instance. This makes it possible to use powerful techniques to locate and combine data that are unavailable to spreadsheets. In your case, you might have 2 related tables, one for your users and one for the images. The relationship between rows in the 2 tables is on the basis of "primary" and "foreign" key fields. If the ID# of user A is 207, then all the rows in the images table that have 207 as the foreign key field will be related to user A. A "schema" for your database might look like this:

Code: Select all

[b]tblUsers[/b]:
    UID           Int  auto-increment (primary key)
    username      varchar  
    userpwd       varchar
    realname      varchar
    emailadd      varchar
    dtjoined      Date
 
[b]tblImages[/b]:
    IID           int  auto-increment (primary key)
    UID           int  (foreign key into tblUsers)
    dirpath       varchar
    filename      varchar
    dtadded       Date
Your learning should begin with a basic database tutorial, then you should choose what database you want to use. MySQL is sort of the default open source robust database, but SQLite is perhaps easier to use and plenty of power for what you're describing. Install your choice and then start studying the specific things about that database. There are just tons of online examples and tutorials for every imaginable database.
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Re: It's a string thing....

Post by MiniMonty »

OK chaps - you've sold me on this.
Can you stay with me for a few days while I implement the whole thing ?

I've got MySQL and phpMyAdmin installed on the server.
I'm actually already using a db for the member system that looks like this:

Image

So where do we go from here ?
About the structure of the site - (which is http://www.shutterbugclub.com/main.php by the way).
When users join up as members a numbered directory is created and within that another called "images".
When users upload pictures they go into that users' images folder and the flash front end pulls them out to
display each users "gallery" so I need to keep that structure in tact. I only mention it because a couple of
tutorials I've looked at use a single "uploads" folder and I won't be able to do that.

I've made a table called "pictures" that looks like this:
Image

Not sure how to do a "foriegn key" or to link the two tables...
What next ?

Best wishes
Monty

PS - it's a Linux server and I'm sitting in front of a nice shiny Mac.
Last edited by MiniMonty on Wed Oct 14, 2009 1:34 pm, edited 1 time in total.
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: It's a string thing....

Post by Mirge »

Sorry for being off-topic.. what font is that on the site currently? It says offline for critical updates.
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Re: It's a string thing....

Post by MiniMonty »

Ooops - link edited :D

The font is Myriad Pro.

Best wishes
Monty
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: It's a string thing....

Post by Mirge »

Oh ok thanks
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: It's a string thing....

Post by califdon »

Oh, then, you're most of the way there. Change one thing in your new table: the length of your UID field (which is the foreign key) must match the length of the primary key field it's related to in the first table, which is int(11). A "foreign key" is a foreign key only in that it matches the field specs and the values of a primary key in another table. It becomes a foreign key whenever you use it in a query to join the 2 tables. So you would use it in queries like:

Code: Select all

// to list all pictures for each member:
$sql="SELECT firstname, lastname, filename, dtadded FROM members LEFT JOIN pictures ON UID=id ORDER BY lastname";
// to list all pictures and which user they belong to:
$sql="SELECT dtadded, filename, firstname, lastname FROM pictures LEFT JOIN members ON id=UID ORDER BY dtadded";
You will need to work out a method to insert the appropriate data in the second table. When a member uploads a picture, your code will need to update the database with a new record in that table. Then, when you want to display pictures, you will be able to identify the owner and all their pictures--their filenames and directory paths--from a query such as the first one, above.
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Re: It's a string thing....

Post by MiniMonty »

Beautiful !

I'm off to learn a lot about uploading pictures and inserting data : )

Best wishes
Monty
Post Reply