Page 1 of 11
Newb advice
Posted: Thu Sep 03, 2009 9:20 am
by MiniMonty
Hi all,
I'm a flash front end designer and I've taken on a project that needs some back end php.
It's not completely greek to me as I write actionscript in flash which is an OO language.
However... I need a script that accomplishes quite a lot of stuff at once and after a lot of
cut and paste from the net I just can't make it work.
So all and any advice, snippets, help, ideas, much appreciated.
I need to do this: (ideally via a Flash front end but html if it has to) !
User uploads an image to a directory called "gallery" (file type and size restricted).
The script counts the images in the directory, then renames the new image to total +1.
i.e. if there are 30 images then the new one becomes 31.jpg.
The script then declares a variable (for flash to read) of the total number of images.
"total_img=31" (or similar).
Finally a success message for the user.
Many thanks
Monty
Re: Newb advice
Posted: Thu Sep 03, 2009 9:26 am
by Ollie Saunders
I need a script that accomplishes quite a lot of stuff at once and after a lot of
cut and paste from the net I just can't make it work.
That's a good genesis for trouble.
To help you better, please show your code and answer these questions:
What errors are you getting?
What is your code actually doing at the moment?
What attempts have you made to fix the problem(s)?
Re: Newb advice
Posted: Thu Sep 03, 2009 11:24 am
by MiniMonty
OK, since posting the original question I've got to the point where I can upload an image and
rename it with a unix date stamp (code attached).
Next step (if anyone can help) would be at the point that the files are renamed, they are given
incremental number values (1.jpg, 2.jpg, 3.jpg etc) instead of a date stamp. I can't find a way to read the directory and
see how many files are there and then to rename them all, or just the latest one appropriately.
All and any help much appreciated (learning a lot today...)
I'm (successfully) using this to upload and rename at present:
- - - - - -
Code: Select all
<?php
//define a maxim size for the uploaded images in Kb
define ("MAX_SIZE","100");
//This function reads the extension of the file. It is used to determine if the file is an image by checking the extension.
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
//This variable is used as a flag. The value is initialized with 0 (meaning no error found) and it will be changed to 1 if an errro occures. If the error occures the file will not be uploaded.
$errors=0;
//checks if the form has been submitted
if(isset($_POST['Submit']))
{
//reads the name of the file the user submitted for uploading
$image=$_FILES['image']['name'];
//if it is not empty
if ($image)
{
//get the original name of the file from the clients machine
$filename = stripslashes($_FILES['image']['name']);
//get the extension of the file in a lower case format
$extension = getExtension($filename);
$extension = strtolower($extension);
//if it is not a known extension, we will suppose it is an error and will not upload the file, otherwize we will do more tests
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
//print error message
echo '<h1>Unknown extension!</h1>';
$errors=1;
}
else
{
//get the size of the image in bytes
//$_FILES['image']['tmp_name'] is the temporary filename of the file in which the uploaded file was stored on the server
$size=filesize($_FILES['image']['tmp_name']);
//compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*1024)
{
echo '<h1>You have exceeded the size limit!</h1>';
$errors=1;
}
//we will give an unique name, for example the time in unix time format
$image_name=time().'.'.$extension;
//the new name will be containing the full path where will be stored (images folder)
$newname="uploads/".$image_name;
//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied)
{
echo '<h1>Copy unsuccessfull!</h1>';
$errors=1;
}}}}
//If no errors registred, print the success message
if(isset($_POST['Submit']) && !$errors)
{
echo "<h1>File Uploaded Successfully!</h1>";
}
?>
Re: Newb advice
Posted: Thu Sep 03, 2009 11:39 am
by Ollie Saunders
Please wrap your code post in the php bbcode. Like this: [syntax=php]code here[/syntax].
Re: Newb advice
Posted: Thu Sep 03, 2009 4:13 pm
by MiniMonty
Ollie Saunders wrote:Please wrap your code post in the php bbcode. Like this:
.
Will do in future. New to this forum so apologies.
Any advice on how to name and number the uploaded files rather than give them a date stamp ?
And then to declare a variable of total number of image files ? (total_imgs=123)
Best wishes
Monty
Re: Newb advice
Posted: Thu Sep 03, 2009 5:06 pm
by jackpf
You could do
Code: Select all
$count = sizeof(glob('directory/*'));
i believe.
Re: Newb advice
Posted: Thu Sep 03, 2009 9:51 pm
by califdon
MiniMonty wrote:Ollie Saunders wrote:Please wrap your code post in the php bbcode. Like this:
.
Will do in future. New to this forum so apologies.
Any advice on how to name and number the uploaded files rather than give them a date stamp ?
And then to declare a variable of total number of image files ? (total_imgs=123)
Best wishes
Monty
Apologies will be accepted if you will edit your original post to do what Ollie requested. The formatting and syntax highlighting will often reveal errors immediately.
Re: Newb advice
Posted: Thu Sep 03, 2009 10:57 pm
by Ollie Saunders
Thanks for saying that califdon. Basically, MiniMonty, I haven't read any of your posts since your first because I can tell from glancing at them you haven't done the things that would make it easy for me to help you. You've got to make it easy for us so that we want to help you (unless you can make it really interesting or enjoyable in another way). Right now you're not making it easy enough for me so I don't really want to help.
Re: Newb advice
Posted: Fri Sep 04, 2009 12:26 am
by Mirge
jackpf wrote:You could do
Code: Select all
$count = sizeof(glob('directory/*'));
i believe.
Best answer so far...
Code: Select all
<?php
print sizeof(glob("/home/mike/Pictures"));
?>
Output: 10
ls -la /home/mike/Pictures
Output:
Code: Select all
mike@mike-kubuntu:~/Pictures$ ls -la /home/mike/Pictures
total 416
drwxr-xr-x 2 mike mike 4096 2009-09-04 00:24 .
drwxr-xr-x 48 mike mike 4096 2009-09-04 00:23 ..
-rw-r--r-- 1 mike mike 70330 2009-07-20 20:53 car1.jpg
-rw-r--r-- 1 mike mike 40585 2009-07-20 20:53 car2.jpg
-rw-r--r-- 1 mike mike 43037 2009-07-20 20:53 car3.jpg
-rw-r--r-- 1 mike mike 38208 2009-07-20 20:53 car4.jpg
-rw-r--r-- 1 mike mike 56147 2009-07-20 20:54 car5.jpg
-rw-r--r-- 1 mike mike 37625 2009-07-20 20:54 car6.jpg
-rw-r--r-- 1 mike mike 39645 2009-07-20 20:54 car7.jpg
-rw-r--r-- 1 mike mike 33339 2009-07-20 20:54 car8.jpg
-rw-r--r-- 1 mike mike 36246 2009-07-20 20:54 car9.jpg
So using sizeof(glob("...")), you can easily determine the name of your next image, minus the extension of course. You'll need to add that.
See
http://us3.php.net/path_info for lots of good information on how to extract different parts of a path/filename.
Re: Newb advice
Posted: Fri Sep 04, 2009 11:56 am
by califdon
Ollie Saunders wrote:Thanks for saying that califdon. Basically, MiniMonty, I haven't read any of your posts since your first because I can tell from glancing at them you haven't done the things that would make it easy for me to help you. You've got to make it easy for us so that we want to help you (unless you can make it really interesting or enjoyable in another way). Right now you're not making it easy enough for me so I don't really want to help.
I think most of the experienced forum contributors feel this way. There's no shame in being a newcomer, but it's too bad that so many people won't take the time to read the forum's rules and apply them, because they discourage the very people who could provide them the most help. Their loss.
Re: Newb advice
Posted: Fri Sep 04, 2009 2:07 pm
by MiniMonty
HI all,
I edited the previous post and wrapped up the code.
And solved the numbering of the uploaded images in this groovy way:
Code: Select all
$groovy = sizeof(glob("/path/to/directory/*"));
$groovy = ++$groovy;
$image_name=$groovy.'.'.$extension;
Re: Newb advice
Posted: Fri Sep 04, 2009 2:08 pm
by jackpf
Awesome.
Re: Newb advice
Posted: Fri Sep 04, 2009 2:17 pm
by MiniMonty
jackpf wrote:Awesome.
LOL - don't take the mick - I'm really new to this !
Re: Newb advice
Posted: Fri Sep 04, 2009 2:18 pm
by Mirge
MiniMonty wrote:HI all,
I edited the previous post and wrapped up the code.
And solved the numbering of the uploaded images in this groovy way:
Code: Select all
$groovy = sizeof(glob("/path/to/directory/*"));
$groovy = ++$groovy;
$image_name=$groovy.'.'.$extension;
You really should use variable names that make sense...
Re: Newb advice
Posted: Fri Sep 04, 2009 2:23 pm
by jackpf
MiniMonty wrote:jackpf wrote:Awesome.
LOL - don't take the mick - I'm really new to this !
I wasn't!!!! I was just happy that my solution worked. Gees. Paranoid.