Jump to content

if file exists, do it, if it doesn't name it "done". Not working


Ninjakreborn

Recommended Posts

Below I have the following code.  It was seeming to work fine, what I need is 2 things.
1. If the file was an attempted upload, it goes ahead and validates, uploads, and moves the file to the proper folder (all of that works)
2. If the file was not an attempted upload, I need the
if (move_uploaded_file($tmp_name1, $target1)) {
$name = $_FILES['file1']['name'];
}else {
$name = "none";
}
here, I need the $name variable to be changed to know.
But it seems whether they try to upload a file ornot, it's not putting none in the database.  Or atleast not all the time, is there something wrong with the logic flow of my code below?
[code]
<?php
if (!empty($_FILES['file1'])) {
$file1 = $_FILES['file1'];
$name = $_FILES['file1']['name'];
$tmp_name1 = $file1['tmp_name'];
$target = $docroot . "/userfiles/"; // prepare target url
$target1 = $target . $name;
if (file_exists($target1)) {
$no = "no";
}else {
if (move_uploaded_file($tmp_name1, $target1)) {
$name = $_FILES['file1']['name'];
}else {
$name = "none";
}
}

} // end file working
?>
[/code]
Link to comment
Share on other sites

If I understand you correctly, if the user does NOT attempt to upload a value you want $name to be set to "none".

the way I read it, the problem is that all of that code is only run if the first conditional (!empty($_FILES['file1'])) is true. So, $name would only be set to "none" if the user uploaded a file and the filemove failed. I think what you want is this:

[code]<?php
if (!empty($_FILES['file1'])) {
    $file1 = $_FILES['file1'];
    $name = $_FILES['file1']['name'];
    $tmp_name1 = $file1['tmp_name'];
    $target = $docroot . "/userfiles/"; // prepare target url
    $target1 = $target . $name;
    if (file_exists($target1)) {
        $no = "no";
    }else {
        if (move_uploaded_file($tmp_name1, $target1)) {
            $name = $_FILES['file1']['name'];
        } else {
            //error handling
        }
    }
} else {
    $name = "none";
}
?>[/code]
Link to comment
Share on other sites

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.