-1

I am trying to create a file in a directory using PHP. Both the file and folder have CHMOD: 777. I am trying to make the file to be called image_ (the file count of a directory + 1) My code is

    <button onclick="<?php 
    error_reporting(E_ALL);
    $directory = __DIR__ . "/images/"; $filecount = 0; $files = glob($directory . "*");  if ($files){ 
    $filecount = count($files); }
    $filecount = $filecount + 1;
    $pagename = 'image_'$filecount;

    $newFileName = './images/'.$pagename.;
    $newFileContent = '<?php echo "TEST"; ?>';
    if (file_put_contents($newFileName, $newFileContent) !== false) {
    echo "File created (" . basename($newFileName) . ")";
    } else {
    echo "Cannot create file (" . basename($newFileName) . ")";
    }
    ?> " class="">Upload</button>

I don't really know PHP and this is an amalgamation of code from different places. What do I need to do to stop the ERROR 500

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
  • You need to _start_ with checking the error log, to find out what actually went wrong. – CBroe Mar 25 '21 at 07:44
  • And also provide us any error you find in logs as mentioned @CBroe. Read following answer to know where to find logs if you are using apache.https://askubuntu.com/a/14767 – Dominik Dosoudil Mar 25 '21 at 12:53
  • @Vicky. Thx but im not trying to make a folder, but a file inside a folder – jay_jay0101 Mar 25 '21 at 16:37
  • @DominikDosoudil the error says ```Parse error: syntax error, unexpected '$filecount' (T_VARIABLE) in /home/vol6_8/epizy.com/epiz_28002566/htdocs/upload.php on line 82 ``` Line 82 is ```$pagename = 'image_'$filecount; ``` – jay_jay0101 Mar 25 '21 at 16:47
  • I think that the problem is missing `.`. Try `'image_' . $filecount` – Dominik Dosoudil Mar 25 '21 at 16:51

1 Answers1

1

You are missing one string concatenation at $pagename = 'image_'$filecount; and you are using one more in $newFileName = './images/'.$pagename.;.

Try to edit your PHP code like this

<?php
    error_reporting(E_ALL);
    $directory = __DIR__ . "/images/"; $filecount = 0; $files = glob($directory . "*");  if ($files){ 
    $filecount = count($files); }
    $filecount = $filecount + 1;
    $pagename = 'image_'.$filecount;

    $newFileName = './images/'.$pagename;
    $newFileContent = '<?php echo "TEST"; ?>';
    if (file_put_contents($newFileName, $newFileContent) !== false) {
    echo "File created (" . basename($newFileName) . ")";
    } else {
    echo "Cannot create file (" . basename($newFileName) . ")";
    }
?>
lpizzinidev
  • 12,741
  • 2
  • 10
  • 29