Jump to content

file_exists with FILE_APPEND


cataiin

Recommended Posts

I have this:

$txt = file_get_contents("http://example?a=".$text);
$file = "texts/".md5($text).".txt";
file_put_contents($file, $txt, FILE_APPEND);

and everything is okay. Except when I access the file again, $file values ​​are doubled. So I try to add if(!file_exists($file)):

$txt = file_get_contents("http://example?a=".$text);
$file = "texts/".md5($text).".txt";
if(!file_exists($file))
{
    file_put_contents($file, $txt, FILE_APPEND);
}

but that does not work, now FILE_APPEND containing only the first $txt values, not all. I hope you understand. Thanks.

Link to comment
Share on other sites

Are you running this in a loop?  What you are stating would only make sense if you are running in a loop.

 

Using "if no file exists" with a FILE_APPEND doesn't really make any sense. Since you are creating the file if it doesn't exist, there is nothing to append. That is why you only have 1 record.

 

Perhaps showing a little more code for context, we can help you get it sorted.

Link to comment
Share on other sites

I believe jcbones nailed the issue on the head, the conditional statement when checking if the file exists is incorrect, and really is not needed here since file_put_contents along with the FILE_APPEND flag already checks if the file exists, and appends the data instead of overwriting if it does.

 

The LOCK_EX flag is more of a precautionary flag and should not fix the issue here, however it should be used if you are expecting heavy traffic in that file.

 

I also don't quite understand the hashing logic here, and as jcbones already stated, your code is most likely in a loop and seeing all of the relevant code will help us pinpoint the problem.

Link to comment
Share on other sites

$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi vitae tortor odio. Proin ac enim a orci consectetur venenatis. Nunc metus nunc, convallis eget tincidunt sit amet, suscipit in urna. Maecenas imperdiet vestibulum malesuada. Nulla quis pharetra neque. Nunc a erat interdum, dictum metus in, dignissim orci.";
$text_split = str_split($text, 25);
foreach($text_split as $texts)
{
    $text_ok = urlencode($texts);
    if(!@file_get_contents("http://example.org/?q=".$text_ok))
    {
        echo "Error.";
    }
    else
    {
        $txt = file_get_contents("http://example.org/?q=".$text_ok);
        $file = "texts/".md5($text).".txt";
        file_put_contents($file, $txt, FILE_APPEND);
    }
}

Thanks for replies. I already tried with LOCK_EX. Same result.

Edited by cataiin
Link to comment
Share on other sites

Is this better:

$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi vitae tortor odio. Proin ac enim a orci consectetur venenatis. Nunc metus nunc, convallis eget tincidunt sit amet, suscipit in urna. Maecenas imperdiet vestibulum malesuada. Nulla quis pharetra neque. Nunc a erat interdum, dictum metus in, dignissim orci.";
$text_split = str_split($text, 25);
$number = 1;
foreach($text_split as $texts)
{
    $text_ok = urlencode($texts);
    if(!@file_get_contents("http://example.org/?q=".$text_ok))
    {
        echo "Error.";
    }
    else
    {
        $txt = file_get_contents("http://example.org/?q=".$text_ok);
        $file = "texts/".md5($text)."-".$number.".txt";
        file_put_contents($file, $txt);
    }
$number++;
}

?

But how to combine the results now?

Edited by cataiin
Link to comment
Share on other sites

First I would check if the files are indeed returning the same values. Then decide the best course of action from there.  After all, it is better to make an informed decision, than to throw things at it in an attempt to get it to "work".

 

 

$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi vitae tortor odio. Proin ac enim a orci consectetur venenatis. Nunc metus nunc, convallis eget tincidunt sit amet, suscipit in urna. Maecenas imperdiet vestibulum malesuada. Nulla quis pharetra neque. Nunc a erat interdum, dictum metus in, dignissim orci.";
$text_split = str_split($text, 25);
foreach($text_split as $texts)
{
    $text_ok = urlencode($texts);
    if(!@file_get_contents("http://example.org/?q=".$text_ok))
    {
        echo "Error.";
    }
    else
    {
        $txt = file_get_contents("http://example.org/?q=".$text_ok);
        $file = "texts/".md5($text).".txt";
        file_put_contents($file, $txt);
    }
 echo $text_ok . ' = ' . $txt . '<br />';
}
Link to comment
Share on other sites

Look, for example this code:

$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi vitae tortor odio. Proin ac enim a orci consectetur venenatis. Nunc metus nunc, convallis eget tincidunt sit amet, suscipit in urna. Maecenas imperdiet vestibulum malesuada. Nulla quis pharetra neque. Nunc a erat interdum, dictum metus in, dignissim orci.";
$text_split = str_split($text, 25);
$number = 1;
foreach($text_split as $texts)
{
    $text_ok = urlencode($texts);
    if(!@file_get_contents("http://example.org/?q=".$text_ok))
    {
        echo "Error.";
    }
    else
    {
        $txt = file_get_contents("http://example.org/?q=".$text_ok);
        $file = "texts/".md5($text)."-".$number.".txt";
        file_put_contents($file, $txt);
    }
$number++;
}

Create texts/eebb32402578e0ce1297b8c2bbac056e-1.txt, texts/eebb32402578e0ce1297b8c2bbac056e-2.txt etc.

Different files with different content. Everything is correct. I only need them "combined" into a single file and I thought to appeal to FILE_APPEND for it because I thought it was easier and escape $number too. And first code do this, makes eebb32402578e0ce1297b8c2bbac056e.txt with content from all files with the same name. But it doubles each time.

 

Sorry but my english is so bad. :( Hope you understand.

Link to comment
Share on other sites

I understand what you are saying, just not why you are trying to do it.

 

What I would do (if I wanted to do this):

 

Use a database.

 

If that wasn't an option:

 

1. write the files. file_put_contents()

2. loop through the files. glob() file_get_contents()

3. see if the contents matched.   strcmp()

4. write the results to a file combining the results.  file_put_contents()

Link to comment
Share on other sites

Part of the problem with obfuscated questions is that we can't really tell what you are trying to do, and it makes it difficult to understand the problem and provide an answer.

 

I hope you have permission to scrape whatever site you are scraping and you are not violating their Terms of Service. If you are, please ignore my answer below.

 

The problem is this line:

$file = "texts/".md5($text)."-".$number.".txt";
You are using THE ORIGINAL TEXT STRING FOR EVERY FILE in the md5() call. I suspect you meant to use $txt there. Which would give you a different file for each result.

 

Also, take that first call to file_get_contents() out of there. You are wasting your resources and resources on the other site:

if (! ($txt = file_get_contents(...))) {
  echo 'Error';
} else {
  # Process $txt
}
Link to comment
Share on other sites

Thanks for replies. I'm allowed to extract data from the site, only $text must be limited to 25 characters.
Following code does exactly what I want:

$txt = file_get_contents("http://example.org/text=".$text_ok);
$file = "texts/parts/".md5($text)."-".$number.".txt";
$file_final = "texts/".md5($text).".txt";
if(!file_exists($file))
{
     file_put_contents($file, $txt);
     file_put_contents($file_final, $txt, FILE_APPEND);
}

I do not know how accurate it is but it's the only solution I've found it...

 

Is no problem with:

$file = "texts/".md5($text)."-".$number.".txt";

Result is always different and content also. I hope I get it right!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.