php 如何将文件保存到其他文件夹中

7rfyedvj  于 2023-03-11  发布在  PHP
关注(0)|答案(1)|浏览(206)

我想将文件保存在其中的文件夹更改为另一个文件夹,但我不知道应该更改什么,例如:
当前网址:example.com/file.zip
校正后:example.ir/up/file.zip
我的代码:

<?php
    if ($_SERVER['REQUEST_METHOD']=="POST") {
set_time_limit(0);
$file_url=$_POST['url_file_url'];
$submitted_password=$_POST["psw_config_pass"];
$original_password=$config_password;
$save_to=basename($file_url)
$target_Path = "up/";
;if ($original_password==$submitted_password) {if (!copy($file_url, $save_to)) {echo "<div class='alert alert-danger'><strong><i class='glyphicon glyphicon-remove'></i>Upload failed!</strong> The file $save_to didn't uploaded on the server.</div>";} else {echo "<div class='alert alert-success'><strong><i class='glyphicon glyphicon-ok'></i>Upload Successful!</strong> The file $save_to has been successfully uploaded on the server.</div>";}}}
    ?>
  
   <div class="container-fluid" id="page_container">
    <header class="text-center page-header jumbotron">
       <span class="h1"><i class="glyphicon glyphicon-transfer"></i></span>
        <h1 class="h1 text-danger"><strong>MY UP</strong></h1>
        <h2 class="h3 text-primary">with <strong>N</strong>.</h2>
    </header>
    <nav class="navbar navbar-fixed-bottom navbar-inverse">
    <div class="container-fluid">
    <div class="navbar-header">
      <a class="navbar-brand" href="#top"><i class="glyphicon glyphicon-download"></i>RedUFL</a>
    </div>
    <div>
      <ul class="nav navbar-nav">
        <li class="active"><a href="#"><i class="glyphicon glyphicon-home"></i>Home</a></li>
        <li><a href="#"><i class="glyphicon glyphicon-globe"></i>Somepage 1</a></li>
        <li><a href="#"><i class="glyphicon glyphicon-user"></i>Somepage 2</a></li> 
      </ul>
    </div>
    </div>
</nav>
    <section>
        <form accept-charset="utf-8" action="index.php" autocomplete="off" method="post" enctype="application/x-www-form-urlencoded" name="frm_upload_from_link" id="frm_upload_from_link" role="form" class="form-horizontal">
        <div class="form-group">
            <label class="control-label col-md-2"><span><i class="glyphicon glyphicon-link"></i>File URL:</span></label>
            <div class="col-md-10">
                <input class="form-control" placeholder="http://example.com/file.zip" name="url_file_url" id="url_file_url" type="url" autofocus required>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-md-2"><span><i class="glyphicon glyphicon-lock"></i>Your Password:</span></label>
            <div class="col-md-10">
                <input class="form-control" placeholder="********" name="psw_config_pass" id="psw_config_pass" type="password" required>
            </div>
        </div>
        <div class="btn-group">
            <button type="submit" name="btn_submit" id="btn_submit" class="btn btn-primary" form="frm_upload_from_link"><i class="glyphicon glyphicon-ok"></i>Upload!</button>
            <button type="reset" name="btn_reset" id="btn_reset" class="btn btn-danger" form="frm_upload_from_link"><i class="glyphicon glyphicon-remove"></i>Reset form.</button>
        </div>
        </form>
    </section>

我试过了

$save_to = 'up'.DIRECTORY_SEPARATOR.$save_to;

在第24行,但它不起作用

eufgjt7s

eufgjt7s1#

试试这个代码

<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {
    $file_url = $_POST['url_file_url'];
    $password = $_POST['psw_config_pass'];
    $save_to = "up/" . basename($file_url);

    if ($password == $config_password) {
        if (copy($file_url, $save_to)) {
            $message = "Upload Successful!";
            $alert_class = "alert-success";
        } else {
            $message = "Upload failed!";
            $alert_class = "alert-danger";
        }
    } else {
        $message = "Incorrect password!";
        $alert_class = "alert-warning";
    }
}
?>

<div class="container-fluid" id="page_container">
    <header class="text-center page-header jumbotron">
        <span class="h1"><i class="glyphicon glyphicon-transfer"></i></span>
        <h1 class="h1 text-danger"><strong>MY UP</strong></h1>
        <h2 class="h3 text-primary">with <strong>N</strong>.</h2>
    </header>

    <nav class="navbar navbar-fixed-bottom navbar-inverse">
        <div class="container-fluid">
            <div class="navbar-header">
                <a class="navbar-brand" href="#top"><i class="glyphicon glyphicon-download"></i>RedUFL</a>
            </div>
            <div>
                <ul class="nav navbar-nav">
                    <li class="active"><a href="#"><i class="glyphicon glyphicon-home"></i>Home</a></li>
                    <li><a href="#"><i class="glyphicon glyphicon-globe"></i>Somepage 1</a></li>
                    <li><a href="#"><i class="glyphicon glyphicon-user"></i>Somepage 2</a></li>
                </ul>
            </div>
        </div>
    </nav>

    <section>
        <form accept-charset="utf-8" action="index.php" method="post" class="form-horizontal">
            <div class="form-group">
                <label class="control-label col-md-2"><span><i class="glyphicon glyphicon-link"></i>File URL:</span></label>
                <div class="col-md-10">
                    <input class="form-control" placeholder="http://example.com/file.zip" name="url_file_url" type="url" autofocus required>
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-md-2"><span><i class="glyphicon glyphicon-lock"></i>Your Password:</span></label>
                <div class="col-md-10">
                    <input class="form-control" placeholder="********" name="psw_config_pass" type="password" required>
                </div>
            </div>
            <div class="btn-group">
                <button type="submit" name="btn_submit" class="btn btn-primary"><i class="glyphicon glyphicon-ok"></i>Upload!</button>
                <button type="reset" name="btn_reset" class="btn btn-danger"><i class="glyphicon glyphicon-remove"></i>Reset form.</button>
            </div>
        </form>

        <?php if (isset($message)): ?>
            <div class="alert <?= $alert_class ?>">
                <strong><i class="glyphicon <?= $alert_class == 'alert-success' ? 'glyphicon-ok' : 'glyphicon-remove' ?>"></i><?= $message ?></strong>
            </div>
        <?php endif; ?>
    </section>
</div>

相关问题