简体   繁体   English

在一个函数中创建多个文件,但它们相互覆盖。 的PHP

[英]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.

相关问题 PHP extractTo无法在一台计算机上正确提取文件,但可以在另一台计算机上工作。 - PHP extractTo does not extract files properly on one computer, but works on the other. Php窗体中的两个组合框相互控制。 怎么样? - Php Two Combo box in a form control each other. How? php查找彼此相邻的两个特殊字符。 - php find two special characters next to each other.` 在 php 中重命名多个文件而不用另一个文件覆盖 - Rename multiple files without overwriting one with another in php 程序生成的div相互堆叠。 如何垂直对齐它们? - Procedurally generated divs are stacking one on top of each other. How can I vertically align them? 防止多个会话在同一网站上用PHP相互覆盖 - Prevent more than one session from overwriting each other on the same website in PHP Php / Ajax / jQuery / Javascript程序可在一个主机上运行,​​而不能在另一个主机上运行。 不可能的 - Php/Ajax/jQuery/Javascript program works on one host and not the other. THE IMPOSSIBLE 在一个相互重叠的文件数据中包含2个php文件 - Include 2 php files in one file data overlapping each other php使用特定文件名保存图像不会相互覆盖 - php save image with specific file name not overwriting each other 从一个表中获取值,然后将其插入另一个表中。 如何? - Get value from one table, insert it in query of other. How to?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM