[英]Creating multiple files in one function but they are overwriting each other. PHP
I am in the process of creating a section of a webpage where a user can upload a photo then manipulate the attributes of said picture, title, caption, etc. My questions comes as this: I have an edit picture function that I create a new caption file in by 我正在创建网页的一部分,用户可以在其中上传照片,然后操纵所述图片,标题,标题等的属性。我的问题是:我有一个编辑图片功能,可以创建一个新的字幕文件由
//create new file
$file = fopen($newinfofile,"w");
//write to file
fwrite($file,$caption);
//close the file
fclose($file);
newinfofile being the files name. newinfofile是文件名。
I also create a new title file the same way but with different names 我还以相同的方式创建了一个新的标题文件,但名称不同
//create new file for title
$file2 = fopen($newinfofile2,"w");
//write to file for title
fwrite($file2,$newtitle);
//close the file for title
fclose($file2);
Later I echo the contents of each file but my problem is that they are echoing the same thing, if I reverse the order and create my title file first they both echo caption info instead. 稍后我回显每个文件的内容,但是我的问题是它们在回显相同的内容,如果我颠倒顺序并首先创建我的标题文件,它们都会回显字幕信息。
So in PHP can you not create and write to two files within the same function? 因此,在PHP中不能在同一函数中创建和写入两个文件吗? Perhaps I am mis-understanding something but I am unsure as to what.
也许我误会了一些东西,但是我不确定。
Edit "sharing the entire function" 编辑“共享整个功能”
//edit file name, picture title, picture caption
function EditRecord($dir, $collection,$picture,$newname,$newtitle,$caption)
{
//set the picture paths
$picpath = $dir . $collection . "/" . $picture;
$newpicpath = $dir . $collection . "/" . $newname;
//set name of file for caption info by changing out JPG for PHP
$infofile = $dir . "Info/" . str_replace("jpg","php", $picture);
$newinfofile = $dir . "Info/" . str_replace("jpg","php", $newname);
//set name of file for title info by changing out JPG for PHP
$infofile2 = $dir . "Info/" . str_replace("jpg","php", $picture);
$newinfofile2 = $dir . "Info/" . str_replace("jpg","php", $newname);
//replace \r with <br>
$caption = str_replace("\r","<br>", $caption);
//rename picture JPG file
rename($picpath, $newpicpath);
//rename info PHP file
rename($infofile, $newinfofile);
//rename 2nd info php file for title
rename($infofile2, $newinfofile2);
//create new file
$file = fopen($newinfofile,"w");
//write to file
fwrite($file,$caption);
//close the file
fclose($file);
//create new file for title
$file2 = fopen($newinfofile2,"w");
//write to file for title
fwrite($file2,$newtitle);
//close the file for title
fclose($file2);
}
The two filesnames you create are exactly the same. 您创建的两个文件名完全相同。
$newinfofile = $dir . "Info/" . str_replace("jpg","php", $newname);
$newinfofile2 = $dir . "Info/" . str_replace("jpg","php", $newname);
both variables will contain the same filename and so write to the same file, consequently overwriting. 这两个变量都将包含相同的文件名,因此将其写入同一文件,因此将被覆盖。
You actually generate same file names in $newinfofile
and $newinfofile2
: 实际上,您在
$newinfofile
和$newinfofile2
生成了相同的文件名:
//set name of file for caption info by changing out JPG for PHP
$infofile = $dir . "Info/" . str_replace("jpg","php", $picture);
$newinfofile = $dir . "Info/" . str_replace("jpg","php", $newname);
is same as 与...相同
//set name of file for title info by changing out JPG for PHP
$infofile2 = $dir . "Info/" . str_replace("jpg","php", $picture);
$newinfofile2 = $dir . "Info/" . str_replace("jpg","php", $newname);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.