开发者

File streaming in PHP - How to replicate this C#.net code in PHP?

I'm writing an interface to a web service where we need to upload configuration files. The documentation only provides a sample in C#.net which I am not familiar with. I'm trying to implement this in PHP.

Can someone familiar with both languages point me in the right direction? I can figure out all the basics, but I'm trying to figure out suitable PHP replacements for the FileStream, ReadBytes, and UploadDataFile functions. I believe that the RecService object cont开发者_如何学Cains the URL for the web service. Thanks for your help!

    private void UploadFiles() {
    clientAlias = “<yourClientAlias>”; 
    string filePath = “<pathToYourDataFiles>”; 
    string[] fileList = {"Config.txt", "ProductDetails.txt", "BrandNames.txt", "CategoryNames.txt", "ProductsSoldOut.txt", "Sales.txt"}; 
    RecommendClient RecService = new RecommendClient();
    for (int i = 0; i < fileList.Length; i++) {
        bool lastFile = (i == fileList.Length ‐ 1); //start generator after last file 
        try {
            string fileName = filePath + fileList[i]; 
            if (!File.Exists(fileName))
                continue;   // file not found
            }
            // set up a file stream and binary reader for the selected file and convert to byte array 
            FileStream fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fStream);
            byte[] data = br.ReadBytes((int)numBytes); br.Close();
            // pass byte array to the web service
            string result = RecService.UploadDataFile(clientAlias, fileList[i], data, lastFile); fStream.Close(); fStream.Dispose();
        } catch (Exception ex) {
            // log an error message
        }
    }
}


For reading files, both on the local system and remotely over HTTP, you can use file_get_contents.

For POSTing to a web service, you should probably use cURL. This article looks like a pretty good explanation of how to go about it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜