problem uploading files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lloydie-t
Forum Commoner
Posts: 88
Joined: Thu Jun 27, 2002 3:41 am
Location: UK

problem uploading files

Post by lloydie-t »

I am having a problem using PHP to upload files. Although it will create the directory it will not upload the file into it. the error message I get is "Warning: open_basedir restriction in effect. File is in wrong directory in /var/www/vhosts/itouch.com/httpdocs/uploadtest.php on line 28"
Any ideas? please find following code. Also safe mode is off

<?php
$name = "$myfile_name";
$folder = "$upload";
$tag = "http://www.itouch.com/support/desk/docs ... lder/$name";
define ("PATH", "/var/www/vhosts/itouch.com/httpdocs/support/desk/docs/user/",TRUE);

include('pageconnect.php');
$connection = @mysql_connect($host, $user, $pass) or die ("Unable to connect to database");
mysql_select_db($db) or die ("Unable to select database: $db ");
?>

<?php
echo "$tag";
?>

<?php
if($name != "") {
if(!$dir = @opendir(PATH.$folder))
mkdir(PATH.$folder,0777)
or die('Directory Could Not Be Created On Server.');
copy($myfile, PATH.$folder."/".$name)
or die('Could Not Copy File To Directory.');
mysql_query("INSERT INTO projdocs (job_id, doctype_id, tag, description, docdate, user_id)
VALUES ('$job_id', '$doctype_id', '$tag', '$description', '$today', '$user_id')")
or die('Can Not Preform Query!');
echo "$name Has Been Uploaded Successfully.";
}
else {
echo 'A File Must Be Selected To Upload.';
}
?>
miso
Forum Newbie
Posts: 1
Joined: Thu Jun 27, 2002 8:55 am

Re: problem uploading files

Post by miso »

lloydie-t wrote:I am having a problem using PHP to upload files. Although it will create the directory it will not upload the file into it. the error message I get is "Warning: open_basedir restriction in effect. File is in wrong directory in /var/www/vhosts/itouch.com/httpdocs/uploadtest.php on line 28"
Any ideas? please find following code. Also safe mode is off

<?php
$name = "$myfile_name";
$folder = "$upload";
$tag = "http://www.itouch.com/support/desk/docs ... lder/$name";
define ("PATH", "/var/www/vhosts/itouch.com/httpdocs/support/desk/docs/user/",TRUE);

include('pageconnect.php');
$connection = @mysql_connect($host, $user, $pass) or die ("Unable to connect to database");
mysql_select_db($db) or die ("Unable to select database: $db ");
?>

<?php
echo "$tag";
?>

<?php
if($name != "") {
if(!$dir = @opendir(PATH.$folder))
mkdir(PATH.$folder,0777)
or die('Directory Could Not Be Created On Server.');
copy($myfile, PATH.$folder."/".$name)
or die('Could Not Copy File To Directory.');
mysql_query("INSERT INTO projdocs (job_id, doctype_id, tag, description, docdate, user_id)
VALUES ('$job_id', '$doctype_id', '$tag', '$description', '$today', '$user_id')")
or die('Can Not Preform Query!');
echo "$name Has Been Uploaded Successfully.";
}
else {
echo 'A File Must Be Selected To Upload.';
}
?>
I think the problem is with the BACK SLASH !
in line 28 :
copy($myfile, PATH.$folder."/".$name)

try this :
copy($myfile, PATH.$folder."//".$name)

since PHP cosiders (back slash / ) as an escape character
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

/ is a forward slash, \ is a back slash - you don't need to escape forward slashes.

Mac
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

lloydie-t:
Have you tried commenting out the open_basedir directive in the php.ini file,

Code: Select all

;open_basedir =
If you have a value set in there then all file operations are limited to the defined directory and below.

Mac
lloydie-t
Forum Commoner
Posts: 88
Joined: Thu Jun 27, 2002 3:41 am
Location: UK

Post by lloydie-t »

Hi twigletmac,
I dont think I will have access to the ini files as my site is hosted. thanks for the pointer though, I will have a word with the hosting company
lloydie-t
Forum Commoner
Posts: 88
Joined: Thu Jun 27, 2002 3:41 am
Location: UK

Post by lloydie-t »

just checked phpinfo ;open_basedir = /my_directory/
I suppose that this needs to be null as the files and dirctory may be create by apache outside of my home directory. Can you confirm.
lloydie-t
Forum Commoner
Posts: 88
Joined: Thu Jun 27, 2002 3:41 am
Location: UK

Post by lloydie-t »

I decided to use move_uploaded_file instead of copy and is working OK except that I now have no permisions to delete uploaded files. Does anyone know how to change the file permisions when uploaded? please find the following code

<?php
if($name != "") {
if(!$dir = @opendir(PATH.$folder))
mkdir(PATH.$folder,0777)
or die('Directory Could Not Be Created On Server.');
move_uploaded_file($myfile, PATH.$folder."/".$name)
or die('Could Not Copy File To Directory.');
mysql_query("INSERT INTO projdocs (job_id, doctype_id, tag, description, docdate, user_id)
VALUES ('$job_id', '$doctype_id', '$tag', '$description', '$today', '$user_id')")
or die('Can Not Preform Query!');
echo "$name has been uploaded successfully.";
}
else {
echo 'A File Must Be Selected To Upload.';
}
?>
lloydie-t
Forum Commoner
Posts: 88
Joined: Thu Jun 27, 2002 3:41 am
Location: UK

Post by lloydie-t »

Dont worry chaps. All sorted now.

<?php
if($name != "") {
if(!$dir = @opendir(PATH.$folder))
mkdir(PATH.$folder,0777)
or die('Directory Could Not Be Created On Server.');
move_uploaded_file($myfile, PATH.$folder."/".$name)
or die('Could Not Copy File To Directory.');
chmod(PATH.$folder."/".$name, 0755);
mysql_query("INSERT INTO projdocs (job_id, doctype_id, tag, description, docdate, user_id)
VALUES ('$job_id', '$doctype_id', '$tag', '$description', '$today', '$user_id')")
or die('Can Not Preform Query!');
echo "$name has been uploaded successfully.";
}
else {
echo 'A File Must Be Selected To Upload.';
}
?>
Post Reply