doujiao1814 2014-05-05 08:16
浏览 40

无法在Ubuntu 12.04中使用PHP移动文件

Hi all this php code is perfectly working on windows but i am unable to move a attached file to a folder in ubuntu using this code.

<?php
$db=mysql_connect("localhost","root","");

mysql_select_db("iphone",$db) or die("Error mysql connection");

$path="event-image/";

$path=$path.basename($_FILES['image']['name']);

if(move_uploaded_file($_FILES['image']['tmp_name'],$path))

{

$ename=$_POST['ename'];
$elocation=$_POST['elocation'];
$date=$_POST['edate'];
$stime=$_POST['stime'];
$etime=$_POST['etime'];
$website=$_POST['website'];
$image=basename($_FILES['image']['name']);
$sql="insert into event(ename,elocation,date,stime,etime,website,image)

 values('$ename','$elocation','$date','$stime','$etime','$website','$image')";

$result=mysql_query($sql,$db);

}

echo 'Message has been sent<br>';



?>
  • 写回答

2条回答 默认 最新

  • dongshen6060 2014-05-05 08:18
    关注

    On File Permissions in Linux

    In Linux web server process is used to run as different user as used for logging in and uploading/storing files. You need to check what user your web server is running as. After that you need to check if that user is permitted to write files into selected target folder.

    Using this command on server will show user name installed web server is running as (requiring privileged access):

    $ sudo ps aux | awk '$11 ~ /apache|lighttpd|nginx|cherokee/ {print $1}' | grep -v root | uniq
    

    After that you might check if target folder is writable by that user (mostly, it's www-data). :

    $ sudo -u www-data /bin/touch -- /path/to/your/file
    

    If this prints "Permission denied" you need to fix permissions on containing folders. This implies the selected target folder and all its parents for the latter might prevent web server from even reading subfolders web server is actually permitted to write into. See this example:

    1. /var is world-readable and world-executable (or world-enterable on folders).
    2. /var/www is world-readbale and world-enterable.
    3. /var/www/private is owner-enterable only and not owned by your web server.
    4. /var/www/private/target is your target folder made web server writable before.

    In this scenario web server won't be permitted to write in /var/www/private/target for it's not even permitted to enter that one's parent folder /var/www/private.

    But don't use chmod 0777 /pathname just because it's the easiest to do!

    Basically I'm used to have my existing folders owned by my login user, but group set to group of web server. If you have folders web server is designed to store files in, you could simply grant write privilege to group on that single folder.

    $ chown myuser:www-data /var/www
    $ find /var/www -type d | xargs chmod 0750
    $ find /var/www '!' -type d | xargs chmod 0640
    $ chmod g+w /var/www/upload
    

    Note:

    1. In first command, www-data isn't name of a user, but name of a group. It's usually same as web server's user name, but it might differ in your case.
    2. /var/www is used as example here and should point to your application's root folder. Subfolder upload is yet another example for subfolder web server might write files into.

    How to select target folders in PHP

    According to your code the target folder is given relatively. In that case you won't find the folder all this checking has to be done in for you don't know what folder your target folder is actually related to. You might try binding your relative target folder's name to the folder your PHP script resides in using some code like this:

    $path = dirname( __FILE__ ) . '/event-image/';
    

    or

    $path = dirname( __FILE__ ) . '/../upload/event-image/';
    

    It depends on your application's folder layout. But this way you are using some absolute pathname for moving file and it's still working in different contexts or on different servers.

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?