Jump to content

Recommended Posts

Basically, when the user submits a page, I want a file to be copied from one directory to another (into the users personal folder on the server) and stay there. But every time the user submits the page, the file is copied again (which I don't want) hence why I placed a if(file_exists).

 

But it seems that even when the file does exist, the PHP still reads on after the 'else'. Anyway - here is my code. Sure would appreciate someone helping me

 

 

<?php

$filename = 'users/$session->username';

$source = "users/progress_clean.php";

$destination = "users/$session->username/progress.php";

 

 

if (file_exists($filename)) {

echo "File Exists -- Display Included: <br />";

include ("users/$session->username/progress.php");

}

// If the file is found - then I DON'T want the rest of the code to continue (it will over write)

 

else {

 

  mkdir("users/$session->username/", 0700);       

  if(copy($source, $destination)) {

      echo "File copied successfully. <br />", "\n";

  include ("users/$session->username/progress.php");

  } else {

      echo "The specified file could not be copied. Please try again.", "\n";

  }}

             

?>

Link to comment
https://forums.phpfreaks.com/topic/206809-php-coping-file-into-new-directory-once/
Share on other sites

By the way, that logic is still flawed. It assumes that if the folder exists that the file exists as well. Why not just check for the file directly instead of just the folder? Plus, you are already defining the $destination and then hard-coding the file path in the IF condition. Way too much work and prone to errors. Use your variables.

 

Also, you have two scenarios where the destination file is displayed. Are you planning on really echo'ing that other text? If not, then the logic can be rewoirked to be more efficient.

 

<?php
$source_file = "users/progress_clean.php";
$destination_folder = "users/{$session->username}";
$destination_file = "{$destination_folder}/progress.php";

if (!file_exists($destination_file))
{
    //Attempt to copy source file
    if(!mkdir($destination_folder, 0700))
    {
        echo "The specified folder could not be created. Please try again.\n";
    }        
    elseif(!copy($source_file, $destination_file))
    {
        echo "The specified file could not be copied. Please try again.\n";
    }
}

//Only include if already present or it copy was successful
if (file_exists($destination_file))
{
    include ($destination_file);
}

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.