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.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度