简体   繁体   中英

Create txt files if not exists php, in a different folder

So basicly I got my php script in one folder, I want to create a txt file in /cache if the file does not exist already. if it exists it should just abort and continue with the rest of the script.

$(document).ready(function() {
    var feed = $("#instagramFeed .wrap");
    $.ajax({
        type: "POST",
        url: "<?= path("instagram_feed.php"); ?>",
        data: { feed_url: "http://iconosquare.com/feed/<?= $butikInstagram ?>", instagram_url: "http://instagram.com/<?= $butikInstagram ?>", cache_file: "<?= $butikInstagram ?>.txt" },
        success: function(data) {
            console.log("SUCCESS");
            feed.html(data).find("img").show();
        }
    });
});

and in my instagram_feed.php i now tried with:

$cache_file = $_POST['cache_file'];
$fh = fopen("cache/"+$cache_file, 'w') or die("fail");

and it returns fail... I got chmod 777 on the cache folder aswell.

What am I doing wrong?

You're trying to concatenate with a + here cache/"+$cache_file .

Sidenote: The + is the JS/C++ concatenate method.

It needs to be a dot. Plus, make sure short tags are set.

You will need to chmod your files also.

In comments you said you used chmod("cache/".$cache_file, 777); that needs to read as chmod("cache/".$cache_file, 0777);

  • The clincher, OP used error reporting as I suggested in comments.

OP: Ok my fault, didnt pass the parameter to the function!

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.

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