Solved

Two simple php questions

Posted on 2006-11-01
8
186 Views
Last Modified: 2006-12-28
Hello,
      i have two simple php questions. 1) I am uploading images and rename each image file to numbers by using time () function.. images name become like this 1162411085.jpg... my question is that what if 100 people are trying to upload files simultaneously... will this cause to upload error? i think php server is multi threaded so it can take many requests at once.

second question.. i am not quite professional in php.. just switched to php.. i want to track members and guests online on the website.. in asp there is an object Application where we store this kind of information in global.asa.. is there anything similiar in php too?

Thanks
0
Comment
Question by:frontpor
  • 2
  • 2
8 Comments
 
LVL 35

Accepted Solution

by:
Raynard7 earned 250 total points
Comment Utility
Hi,

For your first question if you have that many simultanious people trying to upload you may have conflicts.  You could get around this by trying to add the current time to a database - and if it is successfully added you can use that as the file name, otherwise you try again until a unique time is generated.  Alternativley you could append the user's name or another id to the file meaning that you should not get duplicates.

For the second option if you use the $_SESSION array (started with session_start() ) this stores information for the person who is logged in.  If you wanted to track who was online you would need to pass the information for each logged in / guest to a database or repository that you could then query when you load your pages.
0
 
LVL 6

Expert Comment

by:neorush
Comment Utility

You could very well have some problems, as two iamges could be saved in the same second......just use microseconds....

$micro_time = substr(microtime(), 2, 6);
$filename = time().'.'.$micro_time.'.jpg';
echo $filename;

I'll admit this could under a very odd circumstance this could still be a problem. So you could do something like use an md5hash of the user (orginal) filename, plus this time thing....that should make it so there isn't a problem for sure

$filename = md5($_FILES["form_file_name"]["name"]);
$micro_time = substr(microtime(), 2, 6);
$filename = $filename.'.'.time().'.'.$micro_time.'.jpg';
echo $filename;
0
 
LVL 6

Assisted Solution

by:neorush
neorush earned 250 total points
Comment Utility
oh and to answer the session question a little better:

<?PHP

//start save user data....
if(!isset($_SESSION)){
   session_start();
}

if(!isset($_SESSION["username"])){
   $_SESSION["username"] = 'CurrentUsername';
}

echo $_SESSION["username"];

?>

<?PHP

//if you just need to get the user data back ....
if(!isset($_SESSION)){
   session_start();
}
echo $_SESSION["username"];

?>
0
 

Author Comment

by:frontpor
Comment Utility
neorush dont you think this will be a very long name?
$filename = md5($_FILES["form_file_name"]["name"]);
$micro_time = substr(microtime(), 2, 6);
$filename = $filename.'.'.time().'.'.$micro_time.'.jpg';
echo $filename;

i think it would be best to attach id with microsecond.. id is always unique..

my second question was how will get online members and guests number.. when my site starts i start a session.. do i need to add a counter?
0
 
LVL 35

Expert Comment

by:Raynard7
Comment Utility
I think there should be a split betwen neorush and myself
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now