concatenating variables in a string

I have a simple image uploader in PHP, it is uploading images but for some reason it is adding the image in the 'uploads' directory instead of the folder within that directory.

The variable $uploaddir seems to be printing the correct string, I know this because I re-name the image using the $uploaddir and I can see the image is named correctly with the same name as a folder inside the 'uploads' folder.

Iv done a lot of searches on URL strings and variables but cant work this out. Here is my code:

$uploaddir = "uploads/{$site_country}_{$site_state}_{$site_name}";

      $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

      echo "<p>";

      if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        echo "File is valid, and was successfully uploaded.\n";
      } else {
         echo "Upload failed";
      }
      echo "</p>";

Answers


The problem is that you're constructing your file path with a typo in it.

Assuming your file's name is hello.png, then your resulting $uploadfile is currently:

uploads/usa_ny_foohello.png

Whereas you want:

uploads/usa_ny_foo/hello.png

So, what about adding a / at the end of your $uploaddir?

$uploaddir = "uploads/{$site_country}_{$site_state}_{$site_name}/";

Or in a more proper way, you can try this:

$uploaddir = "uploads/{$site_country}_{$site_state}_{$site_name}";
$uploadfile = $uploaddir . '/' . basename($_FILES['userfile']['name']);

I think you need to add /

$uploadfile = $uploaddir .'/'. basename($_FILES['userfile']['name']);

You need to create a directory first

mkdir('uploads/'.$directoryname, 0777, true);

For example

$uploaddir = "uploads/{$site_country}_{$site_state}_{$site_name}/";
if(!is_dir($uploaddir))
{
if (!mkdir($uploaddir, 0777, true)) {
    die('Failed to create folders...');
}

You are missing backslash

$uploaddir = "uploads/{$site_country}_{$site_state}_{$site_name}/";

And you will probably need to create that directory first by

mkdir($uploaddir, 0777, true)


Need Your Help

Paperclip: "has an extension that does not match its content" error

ruby-on-rails paperclip

I am just trying to do some really simple picture uploading using paperclip. I googled this issue and it seems like everyone else has much more complicated problem than simple uploads. Below are my

check sqlite column type

objective-c ios sqlite

I am working on an iOS application that uses a SQLite database.