所以基本上我的 php 脚本放在一个文件夹中,如果该文件尚不存在,我想在/cache 中创建一个 txt 文件。如果存在,它应该中止并继续执行脚本的其余部分。
$(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();
}
});
});
在我的 instagram_feed.php 中我现在尝试使用:
$cache_file = $_POST['cache_file'];
$fh = fopen("cache/"+$cache_file, 'w') or die("fail");
它返回失败...我在缓存文件夹上也得到了 chmod 777。
我做错了什么?
最佳答案
您正在尝试在此处连接 +
cache/"+$cache_file
。
旁注: +
是 JS/C++ 连接方法。
它必须是一个点。另外,请确保设置短标签。
您还需要 chmod 您的文件。
在评论中,您说您使用了 chmod("cache/".$cache_file, 777);
,需要读取为 chmod("cache/".$cache_file, 0777);
- 最重要的是,OP 按照我在评论中的建议使用了错误报告。
OP:好吧,我的错,没有将参数传递给函数!
添加error reporting到文件顶部,这将有助于查找错误。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
旁注:错误报告只能在暂存阶段完成,而不能在生产阶段完成。
关于javascript - 如果不存在 php,则在不同的文件夹中创建 txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30781897/