views:

319

answers:

4

i am trying to write a php upload script to upload files to different folders. My code works for direct path (something like 'path/to/directory') but not for dynamic path taken from runtime.

$directory_self = dirname($_SERVER['PHP_SELF']);
$folder = $_POST['folder_name']; //final folder
$toupload = $_SERVER['DOCUMENT_ROOT'] . $directory_self .'/files'. $folder;
$uploadsDirectory = str_replace (" ", "", $toupload);

When i echo $uploadsDirectory it shows the exact path. Could any one help me what could be wrong in this?

A: 

Try using dirname(__FILE__);

<?php
$directory_self = dirname(__FILE__);
$folder = "faruk"; //final folder
$toupload = $_SERVER['DOCUMENT_ROOT'] . $directory_self .'/files/'. $folder;
$uploadsDirectory = trim($toupload);
echo $uploadsDirectory."\n";

?>

Output on my laptop;

/home/test/Desktop/files/test
streetparade
i cannot use dirname(__FILE__) because i am working on windows server which results in backward slash for $directory_self and forward for document root.
Devdarsh
@Devdarsh - so, use regex or a string replacement method to fix that.
Moshe
A: 

You should check and see if the folder is created and if the script has permission to write files there.

What is the exact output of the upload script? (i.e. what errors does it throw?)

Alex Ciminian
Write permission is there and i can write files by giving direct path. I have error only on defining $_SERVER['DOCUMENT_ROOT'] . $directory_self .'/files'. $folder;It throws "An error has occured: receiving directory insuffiecient permission..."
Devdarsh
$folder will change at run time
Devdarsh
I still don't understand :). Does the _generated_ string correspond to an existing folder or not? If you try to upload to `$_SERVER['DOCUMENT_ROOT'] . $directory_self .'/files'. $folder`, `$folder` is `/abc` and `[...]/abc` does not exist you get an error.
Alex Ciminian
@Devdarsh - Check your permissions on the document itself. Creating web files in Unix/Linux caused permissions issues for me in the past. You need to check that not only is the overall path okay in terms of permissions, but that each part of your path has proper permissions as well.
Moshe
Yes, the folder presents in the server and is writable. $folder will assign the user selected folder which is already created.
Devdarsh
A: 

Try adding some debug stuff to see if the path you're generating actually exists and is writeable:

$directory_self = dirname($_SERVER['PHP_SELF']);
$folder = $_POST['folder_name']; //final folder
$uploaddir = $_SERVER['DOCUMENT_ROOT'] . $directory_self . '/files';

$uploadsDirectory = str_replace (" ", "", $uploaddir);

if (!is_dir($uploadDirectory)) {
    die("$uploadDirectory is not a directory");
}

if (!is_writeable($uploadDirectory)) {
    die("$uploaddir is not writeable");
}

$toupload = $uploadDirectory . $folder;

if (!is_writeable($toupload)) {
    die("$toupload is not writeable");
}
Marc B
Hi, i tried your suggestion, it throws "E:/TechPlat/apache/htdocs/uploads/abc is not a directory". But i have this folder created and i can load the printed path in the explorer bar and it loads the folder. I am using windows server.
Devdarsh
Is E: a local drive or a network share? Are you running this off a LiveCD of some sort and E: is the cdrom? Does the user account that Apache's running under have access to that folder? Even if you do, Apache might not. Try backing "up" the tree, one level at a time, and see if any of the parent directories work. .../htdocs/uploads, then .../htdocs, etc....
Marc B
A: 

did u get it working,i too havethe same requirement,i.e assigning the path dynamically... please post the full code i am new to php

please

mayuri