Search code examples
phpcsvsyntaxfwritemkdir

What errors do I have in my php code to create a new directory and then save a file in that new directory


So, I have been trying to de bug my code for hours now, and I whilst I am not a developer, and very novice to php, I cannot find my syntax or logic error:

In general, I am trying to develop a code that will create a new directory based upon some posted fields from an html form.

The abridged version of my code is this:

$inputA = 'Something Red';
$inputB = '_Old';
$inputC = '_metal';
$inputD = '_100';

$trimmedInputA = str_replace(' ', '', $InputA);
$dirStructure = '/folderName/'.$trimmedInputA;
if (mkdir($dirStructure,0777, true)){
    die('Failed to create folders...');
};

///////skipping ahead  -->  -->  -->

$file=fopen('folderName/'.$trimmedInputA.'/'.$trimmedInputA.$inputB.$inputC.$inputD.'.csv','w');
 if(!$file){
    die ("Failed to Create File");
 }

fwrite($file,$csv_data);

I am getting the second 'die' warning, and the custom folder is not being created.

The end game, taken from this example is to create a file with a URL:

http://www.domain.com/folderName/SomethingRed/SomethingRed_Old_metal_100.csv

Any ideas on my speed-bumps?

I swear I had it working yesterday, but did some other alterations to the php, and I can't remember what I did or un-did


Solution

  • Compare, what you assign to $dirStructure and $file.

    You can notice lack of leading / in fopen parameter.

    Nevertheless,

    echo('folderName/'.$trimmedInputA.'/'.$trimmedInputA.$inputB.$inputC.$inputD.'.csv');
    

    would certainly help you to debug.