Reputation: 312
I am trying to do a php upload that will upload into a specific folder. One would choose the file they wish to upload next to a dropdown box which is a folder list. This is because it organises files.
<?php
session_start();
if(!isset($_SESSION["USER"]["Admin"])){
header("Location: index.html?unath");
}
$folder = mysql_real_escape_string($_POST['loc']);
$target_path = "../../shared/docs/$folder";
$upload2 = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $upload2)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>
Currently the code uploads the file into the "docs" folder and not docs/folder. Instead it puts the folder name in front of the file. For example- if the folder is called "abc" and my file is called robs.docx it will upload it to the main Docs folder and call it abcrobs.docx
Upvotes: 0
Views: 2631
Reputation: 1277
You should properly escape your variables:
$target_path = "../../shared/docs/". $folder ."/";
Upvotes: 0
Reputation: 522567
mysql_real_escape_string
because there's no SQL involved here.mysql_real_escape_string
returns null
. So you're probably throwing away the $_POST['loc']
value.var_dump
liberally to see what your values look like at various stages and do some debugging.Upvotes: 1
Reputation: 2895
Add a /
on the end of your $target_path
:
$target_path = "../../shared/docs/$folder/";
Upvotes: 0
Reputation: 10346
You have a missing slash
Replace this line:
$upload2 = $target_path . basename( $_FILES['uploadedfile']['name']);
with:
$upload2 = $target_path ."/". basename( $_FILES['uploadedfile']['name']);
OR:
Replace this line:
$target_path = "../../shared/docs/$folder";
with:
$target_path = "../../shared/docs/".$folder."/";
Upvotes: 2