URL to parse

Hello,

I’m sorry, my english is bad

$url=‘http://www.site.com/1111/2222/3333/4444/files.xxx’;

The directory path must
result1: /1111/2222/3333/4444/
AND
result2: 1111/2222/3333/4444

array($result1);

array($result2);

How do I do it?

I beginner

Thanks

Hi Adem,

There are several different ways to accomplish what you are looking for.

If your url will always contain the http://…/ prefix, the following should do what you want:[php]$path = explode(’/’,dirname($url),4);
$result1 = ‘/’.$path[3].’/’;
$result2 = $path[3];[/php]
Note that I am setting the $result1 and $result2 as variables, not arrays. If you are really looking for the results to be a single element array, use the following instead:[php]$path = explode(’/’,dirname($url),4);
$result1 = array(’/’.$path[3].’/’);
$result2 = array($path[3]);[/php]
Let me know if it isn’t working as expected.

@malasho,

Thank you very much

Best Regards

My pleasure Adem,

Good luck with your project.

Forgot One Thing

URLs are array()

Problem?

That shouldn’t be a problem, but it will take a little more code.

Do you need to loop through an array of urls and get the required path elements for each of them, or is $url an array with only one element?

Depending on what you have, one of the following approaches should work:

If $url is a single element array (with a key of 0):
[php]$url_new = $url[0];

$path = explode(’/’,dirname($url_new),4);
$result1 = ‘/’.$path[3].’/’;
$result2 = $path[3];[/php]

If $url is an array with multiple urls that each need to be processed:[php]if(is_array($url) && !empty($url)) foreach($url as $url_new) {
$path = explode(’/’,dirname($url_new),4);
$result1 = ‘/’.$path[3].’/’;
$result2 = $path[3];

// Insert code for each url here
}[/php]
Hope this makes sense.

Jay

Forgive me

I can not run

[php]$url = array(
http://www.site.com/ss/ss/ddddd/aaaaa/aaaa.jpg’,
http://www.site.com/sskkss/ddddd/aaaaa/aaffaa.zip’,
http://www.site.com/ssttss/ddddd/aaa/aa/aaffaa.rar’,
http://www.site.com/ssggss/dd/ddd/aaaaa/aaaa.php’,
http://www.site.com/aasss/ddddd/aaaaa/aaaa.html’,
);[/php]

Adem,

I just tested it with your array and it ran fine on my server. Here is what I used to test it…[php]<?php
$url = array(
http://www.site.com/ss/ss/ddddd/aaaaa/aaaa.jpg’,
http://www.site.com/sskkss/ddddd/aaaaa/aaffaa.zip’,
http://www.site.com/ssttss/ddddd/aaa/aa/aaffaa.rar’,
http://www.site.com/ssggss/dd/ddd/aaaaa/aaaa.php’,
http://www.site.com/aasss/ddddd/aaaaa/aaaa.html’,
);

if(is_array($url) && !empty($url)) foreach($url as $url_new) {
$path = explode(’/’,dirname($url_new),4);
$result1 = ‘/’.$path[3].’/’;
$result2 = $path[3];

echo $result1.’
’.$result2.’

’;
}
?>[/php]

And here is the output:[code]/ss/ss/ddddd/aaaaa/
ss/ss/ddddd/aaaaa

/sskkss/ddddd/aaaaa/
sskkss/ddddd/aaaaa

/ssttss/ddddd/aaa/aa/
ssttss/ddddd/aaa/aa

/ssggss/dd/ddd/aaaaa/
ssggss/dd/ddd/aaaaa

/aasss/ddddd/aaaaa/
aasss/ddddd/aaaaa
[/code]

Please try this test and let me know if it doesn’t output the above results.

Again,

@malasho,

Thank you very much

Best Regards

My pleasure!

Let me know if you run into any other issues with it.

Again, I ask you for a help

1. This script: Reads links from a file
[php]<?php
// Reads links from a file
$read_dir="./";
$read_file=“links_data.txt”;
$connect_to_red=@fopen ("$read_dir/$read_file",‘r’);
if (!$connect_to_red) {
echo “could not open database”;
exit();
}
while (!feof ($connect_to_red) ) {
$read=fgets ($connect_to_red,1024);
echo “$read\n”;
}
@fclose($connect_to_red);
?>[/php]

2. This script: Creates folders of Links
[php]<?php
// Creates folders of Links
$url = array(
http://www.domain.com/file.php’,
http://www.domain.com/dir1-1/dir2/dir3/file.jpg’,
http://www.domain.com/dir1-2/dir2/dir3/dir4/file.zip’,
http://www.domain.com/dir1-3/dir2/dir3/dir4/dir5/file.gif’,
http://www.domain.com/dir1-4/dir2/dir3/dir4/dir5/dir6/file.png’,
http://www.domain.com/dir1-5/dir2/dir3/dir4/dir5/dir6/dir7/file.rar’,
);

if(is_array($url) && !empty($url)) foreach($url as $url_new) {
$path = explode(’/’,dirname($url_new),4);
$dirspath = array(’./’.$path[3].’/’);
$dirspath2 = array($path[3]);

for($i=0;$i<count($dirspath);$i++){
$fullDir = $dirspath[$i];

exec("mkdir -p ".$fullDir);

$dirs=explode("/",$fullDir);
$currentFolder=’’;
for ($x=0; $x<count($dirs); $x++){
$currentFolder.=$dirs[$x].’/’;
if(!is_dir($currentFolder)){
echo "Created: $currentFolder
";
if(!mkdir($currentFolder)){ die("Could not make ".$currentFolder); }
}
}
}
}
?>[/php]

3. This script: Moved files of Links
[php]

    <?php // Moved files of Links $url = array( 'http://www.domain.com/file.php', 'http://www.domain.com/dir1-1/dir2/dir3/file.jpg', 'http://www.domain.com/dir1-2/dir2/dir3/dir4/file.zip', 'http://www.domain.com/dir1-3/dir2/dir3/dir4/dir5/file.gif', 'http://www.domain.com/dir1-4/dir2/dir3/dir4/dir5/dir6/file.png', 'http://www.domain.com/dir1-5/dir2/dir3/dir4/dir5/dir6/dir7/file.rar', ); $record_directory = './test_folder/'; for($i=0;$i<count($url);$i++){ $file_name = basename($url[$i]); if(copy($url[$i],$record_directory.$file_name)){ echo '
  1. File Uploaded: '; echo $file_name; echo '
  2. '; }else{ echo '
  3. error
  4. '; } } ?>
[/php]

Is it possible to run scripts by combining numbered 1,2,3?
I will use to move to my website from the server to another server.

you can also

This script does not list the folders named “0”. I wonder what could be the problem?

[php]<?php
// Lists directory
error_reporting(“E_ALL & ~E_NOTICE”);
set_time_limit(0);
function lists ($directory)
{
$slash = “/”;
$dir = dir($directory);
while ($x = $dir->read())
{
if ($x != “.” && $x != “…”)
{
if (is_dir($directory.$slash.$x))
{
lists($directory.$slash.$x);
}
else
{
if ($x != “.” && $x != “…”)
{
$data0="’";
$data1=‘http://’;
$data2= $_SERVER[‘HTTP_HOST’];
$data3=’/’;
$data4= $directory.$slash.$x;
$data5=’’’;
$data6=",";
$data7="
";
$data= $data0.$data1.$data2.$data3.$data4.$data5.$data6;
////////////////////////////////////////////
$file_name = “links_data.txt”;
$value_write = “$data\n”;
if ($data) {
if (!file_exists($file_name)){
touch($file_name);
chmod($file_name,0666);
}
$connect_to_file = fopen($file_name,“a+”);
if (!fwrite($connect_to_file,$value_write)){
echo “Write to file.
”;
exit;
}
echo “Successfully written. See: >> links_data.txt
”;
} else {
echo “Write to file.
”;
}
////////////////////////////////////////////////
}
}
}
}
$dir->close();
}

lists(’./’);
?> [/php]

Best Regards

Can you help please?

Thanks

Adem,

Sorry, I had missed your reply somehow.

I will look at it for you today. Since you say your are moving to a different server, you may want to look at this:
http://campstamba.com/2010/12/create-your-own-php-backup-solution-complete-tutorial-with-source-files/

If zip is available to you (it usually is), this would seem like a far better option. The link I gave provides an example of using zip for such a purpose, as well as an approach to moving your databases as well.

Hi,
I server “SSH, Telnet” and “FXP FTP transfers from server to server” does not support,
Such as the ports that a very complex
My codes very simple and practical

I codes just need to combine, you request

Thanks

adem,

In order to combine the scripts, we will need to put the links in script 1 into an array. Depending on how your file is formatted, this could be as simple as changing…[php]echo “$read\n”;[/php]to…[php]$url[]=$read;[/php]

Can you provide me with a sample of the output from script 1.

The reason your fourth script is not displaying directories named “0” is because of the following line:[php] while($x = $dir->read())[/php]

Try changing it to…
[php]while(false !== ($x = $dir->read()))[/php]

The issue is that a filename of 0 evaluates to false with the original code. When dealing with a situation like this you need to specify the identical comparison operator to differentiate between 0 and false.

This problem is all right.
I am inexperienced. Can you combine the other codes?

i can’t thank you enough.

Example my file

'http://www.domain.com/file.php',
'http://www.domain.com/dir1-1/dir2/dir3/file.jpg',
'http://www.domain.com/dir1-2/dir2/dir3/dir4/file.zip',
'http://www.domain.com/dir1-3/dir2/dir3/dir4/dir5/file.gif',
'http://www.domain.com/dir1-4/dir2/dir3/dir4/dir5/dir6/file.png',
'http://www.domain.com/dir1-5/dir2/dir3/dir4/dir5/dir6/dir7/file.rar',

OR

'http://www.domain.com/file.php http://www.domain.com/dir1-1/dir2/dir3/file.jpg http://www.domain.com/dir1-2/dir2/dir3/dir4/file.zip http://www.domain.com/dir1-3/dir2/dir3/dir4/dir5/file.gif http://www.domain.com/dir1-4/dir2/dir3/dir4/dir5/dir6/file.png http://www.domain.com/dir1-5/dir2/dir3/dir4/dir5/dir6/dir7/file.rar

It doesn’t matter
How do I save it if necessary

Again thanks

OK, if all three scripts work as it, it looks like you should be able to combine them like this…[php]<?php
function readUrls()
{
// Reads links from a file
$read_dir="./";
$read_file=“links_data.txt”;
$connect_to_red=@fopen ("$read_dir/$read_file",‘r’);
if(!$connect_to_red)
{
echo “could not open database”;
exit();
}
while(!feof($connect_to_red))
{
$read=fgets($connect_to_red,1024);
$url[] = $read;
}
@fclose($connect_to_red);
return $url;
}

function makeFolders($url)
{
// Creates folders of Links
if(is_array($url) && !empty($url))
foreach($url as $url_new)
{
$path = explode(’/’,dirname($url_new),4);
$dirspath = array(’./’.$path[count($path)-1].’/’);
$dirspath2 = array($path[count($path)-1]);

  for($i=0;$i<count($dirspath);$i++)
    {
      $fullDir = $dirspath[$i];
      exec("mkdir -p ".$fullDir);
      $dirs=explode("/",$fullDir);
      $currentFolder='';
      for($x=0;$x<count($dirs);$x++)
        {
          $currentFolder.=$dirs[$x].'/';
          if(!is_dir($currentFolder))
            {
              echo "Created: $currentFolder <br/>";
              if(!mkdir($currentFolder))
                {
                  die("Could not make ".$currentFolder);
                }
            }
        }
    }
}   

}

function moveLinks($url)
{
// Moved files of Links
echo ‘

    ’;

    $record_directory = ‘./test_folder/’;
    for($i=0;$i<count($url);$i++)
    {
    $file_name = basename($url[$i]);
    if(copy($url[$i],$record_directory.$file_name))
    {
    echo '

  1. File Uploaded: ';
    echo $file_name;
    echo ‘
  2. ’;
    }
    else
    {
    echo ‘
  3. error
  4. ’;
    }
    }
    echo ‘
’;
}

$urls = readUrls();
makeFolders($urls);
moveLinks($urls);

?>[/php]

This creates an array called $urls from the code in your first script. It then passes this array to the code from your second script. Finally, it passes these $urls to the code from your third script.

If that isn’t what you want, let me know.

I cannot combine because of I am beginner and inexperienced.

I have a links_data.txt file that links of all files of my website recorded in.
(I can record links how it must be.)
I want to do the following:

When I run the script that were 3 different on new server, the script will read the links from the file and create a folder and move the files.

Note: No problem database backup

I am sorry, I occupied you very much.

Sponsor our Newsletter | Privacy Policy | Terms of Service