0

I have a simple .php file:

<?php
$archivo = $_POST["name"];
$alineamiento = $_POST["alineamiento"];
$fp=fopen("$archivo","w");
fwrite($fp,$alineamiento);
fclose($fp);
?>

This works fine, but when I need to write the file in a subfolder (sub_mat):

<?php
$archivo = $_POST["name"];
$alineamiento = $_POST["alineamiento"];
$fp=fopen("/var/www/SChip/sub_mat/$archivo","w");
fwrite($fp,$alineamiento);
fclose($fp);
?>

this doesn't work. I also try it without the absolute path dir, like:

<?php
$archivo = $_POST["name"];
$alineamiento = $_POST["alineamiento"];
$fp=fopen("sub_mat/$archivo","w");
fwrite($fp,$alineamiento);
fclose($fp);
?>

I have rw permissions in the sub_mat folder. I try this with Chrome, FF, and Opera, and it doesn't work. I know this must be silly but I can't figure out the problem.

EDIT

I modified the script, adding system("mv ...") and initializing the values. If I run this in the Console it works fine, no errors, only:

PHP Notice: Undefined index: name in /var/www/SChip/create-matrix.php on line 2
PHP Notice: Undefined index: alineamiento in /var/www/SChip/create-matrix.php on line 3

but that's obvious... But when I run in Chrome or FF it doesn't work, no errors or warnings. It just leaves the file in the current folder, and the mv is not executed.

<?php
$archivo = $_POST["name"];
$alineamiento = $_POST["alineamiento"];
$archivo = "testing";
$alineamiento = "test2";
$fp=fopen("$archivo","w");
fwrite($fp,$alineamiento);
fclose($fp);
system("mv $archivo custom_matrices_temporal/$archivo");
?>
Wiseguy
  • 20,522
  • 8
  • 65
  • 81
user1843376
  • 535
  • 2
  • 7
  • 12
  • 4
    You need to provide people with the error if you want help. – thatidiotguy Feb 06 '13 at 20:31
  • 6
    fyi: not a good idea to allow php to write to user supplied filename. you could have your index file replaced with a shell script – Lawrence Cherone Feb 06 '13 at 20:34
  • 1
    If not getting an error (should come thru at warning level), ensure the *filename* is not also a directory. See note on [fopen](http://php.net/manual/en/function.fopen.php) page and `is_dir()`. – ficuscr Feb 06 '13 at 20:37
  • 1
    If the file doesn't already exist, I believe the directory needs _execute_ permissions as well to allow the creation of new files. – Wiseguy Feb 06 '13 at 20:47
  • yes that's was the problem... thank you – user1843376 Feb 06 '13 at 20:51
  • 2
    Please, please check the comment by @LawrenceCherone this script is **very** dangerous. People can overwrite your files or inject malicious code into them – thaJeztah Feb 06 '13 at 21:49

1 Answers1

3

The directory needs execute (x) permissions as well to allow the creation of new files.

Wiseguy
  • 20,522
  • 8
  • 65
  • 81