简体   繁体   中英

PHP Create Folder, Create Subfolder using #ID and Upload a default file?

Alright so I've had a bit of a go in my mind about this but I didn't really want to start without knowing how I was going to do it...

I will be providing basic automatic hosting on my hosting account, where as the user will check the website, pay for a hosting package using PayPal and IPN does what it does. That I already have in place and working. However, how would I type a script which would automatically upon payment - Create a Folder using the NEXT AUTO_INCREMENT #ID number in say table WebDesign_HostingAccounts . So if the next ID to be used was say #0005, the folder would be named '0005' and in that folder then automatically add a Public_HTML subfolder and upload a Welcome.PHP page.

I know it would have something to do with the following script clips:

<?php
// Desired folder structure
$structure = './depth1/depth2/depth3/';

// To create the nested structure, the $recursive parameter 
// to mkdir() must be specified.

if (!mkdir($structure, 0777, true)) {
    die('Failed to create folders...');
}
// ...
?>

But how would I add that in to a MySQL Query to check the next ID and then upload a welcome page?

Any basic scripts would be of much help! Thanks!

I've been doing this back to my previous freelance projects which I found from my experience as a bad practice.

Let's skip the public_html and Welcome.php parts first...

Here's the "scenario" part

What you're trying to do is:

  1. PHP first (by trying to determine the next AUTO_INCREMENT ID and create a folder based on this "next" auto_increment ID)
  2. Then you do the INSERT commands in your MySQL database.

Now here's the question: What if you have 2 clients and requested the same request simultaneously?

Client 1 is expecting that the next auto_increment ID is 2

Client 2 is also expecting that the next auto_increment ID is 2 (not 3 because there's not yet a row from the database with 2 as its primary ID, so it should also get 2 ).

The epic part is, if Client 2 made it first to finish its request. I'm pretty sure Client 1 will soon finish its process too, and it will try to create an AUTO_INCREMENT ID-based folder, but there's already an existing folder for the AUTO_INCREMENT ID-based. There's the redundancy part. Epic, fail.

And here's the Main event, "The Solution"

Let's do this the other way,

  1. You do the INSERT command/s in MySQL database, I suggest you use Transaction-based process.
  2. The PHP will then create a folder, with name based on the resulting AUTO_INCREMENT ID from the recently inserted row.

What the hell? How do I get the AUTO_INCREMENT ID from the recently inserted row?"

Use LAST_INSERT_ID(), a built-in function in MySQL to get the AUTO_INCREMENT ID from the last inserted row, (which I guess) from the last table your inserted a certain row. Now here's the reading part regarding this LAST_INSERT_ID() thing:

Will it make a conflict if other clients also inserted a row to that table?

No, because each client has their own distinct sessions. Which means it only returns the auto-increment ID from the last inserted row of current client, not by global reference. (Hooray for MySQL engine!)

Multi-Insert Query warning

LAST_INSERT_ID() returns the ID of the first inserted row if ever you're thinking of using a multi-insert query like this:

INSERT INTO mytable(name, age)
VALUES
    ("Mark Hernandez", 25),
    ("Allen Linatoc", 20)
;

It will only return the insert id of the first row you specified in your multi-insert query.

You can do it faster with php's mysql_insert_id . I don't know what Database abstraction/extension (PDO, MySQL, MySQLi) you're using, but here's an example from PHP's native mysql extension: http://php.net/manual/en/function.mysql-insert-id.php

At the end of the day, it's always best to refer to the documentations for any clarification you need.

Anyway, with your part saying:

and in that folder then automatically add a Public_HTML subfolder and upload a Welcome.PHP page.

  • I guess that welcome.php file should be your service's job to do ;)
  • public_html? Take note of the chmod stuff, you know the filesystem permission thing. Security should always come first. File system permissions are just another subject. You need to raise another question for this.

I hope I helped you sort things out. Now let's pass the correction to experts ^_^

Have a good day!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM