简体   繁体   English

通过PHP将文件上传到服务器

[英]Upload a file to server via PHP

I create an .ics file on a PHP script on my website, now I am saving that file locally in my computer, but I would like to upload it to my iCal Exchange account so i can share it from there. 我在网站上的PHP脚本上创建了一个.ics文件,现在我将该文件保存在本地计算机中,但是我想将其上传到我的iCal Exchange帐户,以便可以从那里共享它。

I can uploaded via iCal (the app) but I need to do it through PHP. 我可以通过iCal(该应用程序)上传,但我需要通过PHP进行上传。 In iCalExchange i have an username and password and the instruction say: 在iCalExchange中,我有一个用户名和密码,说明中说:

To publish a new calendar from iCal, select the "Publish on a Web server" option, and use one of the URLs: 要从iCal发布新日历,请选择“在Web服务器上发布”选项,并使用以下URL之一:

http://icalx.com/private/zeroan/ http://icalx.com/private/zeroan/

http://icalx.com/public/zeroan/ http://icalx.com/public/zeroan/

Be sure to enter your new username and password correctly. 确保正确输入新的用户名和密码。

I tried this in PHP with no luck: 我在PHP中试过了运气:

$ftp_server='74.91.122.152';//serverip
    $conn_id = ftp_connect($ftp_server); 


    // login with username and password
    $user="zeroan";
    $passwd="****";
    $login_result = ftp_login($conn_id, $user, $passwd); 

// check connection
   if ((!$conn_id) || (!$login_result)) { 
        echo "FTP connection has failed!";
        echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
        die; 
    } else {
        echo "<br>Connected to $ftp_server, for user $user<br>";
    }
//directorylike /www.velibaba.com/images
  ftp_chdir($conn_id, "http://icalx.com/public/zeroan/");


//$destination_file=ftp_pwd($conn_id);


$source_file='cale.ics';
$destination_file="calendario.ics";
echo ("<br>");
print $destination_file;

echo ("<br>");

// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); 

// check upload status
if (!$upload) { 
        echo "FTP upload has failed!";
    } else {
        echo "Uploaded $source_file to $ftp_server as $destination_file";
    }

// close the FTP stream 
ftp_close($conn_id); 

Thanks 谢谢

Full working example: My /var/www/stackoverflow/curluploader.php file 完整的工作示例:我的/var/www/stackoverflow/curluploader.php文件

<?php 
$url = "http://localhost/stackoverflow/processupload.php";
$post_data['name'] = "Foo";
$post_data['file'] = '@'.__DIR__ . '/file.txt';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$response = curl_exec($ch);

echo $response;

Prefixing filepath with @ does the trick. 用@前缀filepath可以解决问题。

Now my /var/www/stackoverflow/processupload.php 现在我的/var/www/stackoverflow/processupload.php

<?php
echo "<pre>";
print_r($_POST);
print_r($_FILES);

If everthing it's okay you should something like: 如果一切正常,您应该执行以下操作:

Array
(
    [name] => Foo
)
Array
(
    [file] => Array
        (
            [name] => file.txt
            [type] => application/octet-stream
            [tmp_name] => /tmp/phpEBgYXy
            [error] => 0
            [size] => 30
        )

)

Exclude proccessupload authentication, but send user and password the same way you post field 'name', make a little auth on proccessupload to keep things safe. 排除proccessupload身份验证,但以与您发布字段“名称”相同的方式发送用户名和密码,对proccessupload进行一点身份验证以确保安全。 Be sure the apache user has write privileges on remote folder. 确保apache用户对远程文件夹具有写权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM