Witam znalazłem script php i aplikacje w silverlight do wysyłania pliku na server, problem polega na tym że działa wszystko gdy php jest na localhost lecz gdy wrzucę script na jakiś hosting np cba to po wysłaniu nie ma na nim pliku. Może ktoś wie dlaczego? Poniżej dodaje kod
PHP
<?php
// This is the most basic of scripts with no try catches
$filename = isset($_REQUEST["filename"]) ? $_REQUEST["filename"] : "jjj";
$append = isset($_REQUEST["append"]);
try
{
if(!$append)
$file = fopen($filename,"w");
else
$file = fopen($filename,"a");
$input = file_get_contents ("php://input");
fwrite($file,$input);
fclose($file);
}
catch (Exception $e)
{
echo 'Caught exception: ', $e->getMessage(), "\n";
}
?>
c#
public partial class MainPage : UserControl
{
/// <summary>
/// Chunk size in bytes: file will be uploaded in blocks of these. The smaller thet size, the more accurate the upload progress bar and the more requests there will be made to the webserver
/// </summary>
public const int CHUNK_SIZE = 4096;
/// <summary>
/// URI to where thet upload is made
/// Set it to either <yourHost>/FileUpload.ashx?filename={0}&append={1} or <yourHost>/FileUpload.php?filename={0}&append={1}
/// </summary>
public const string UPLOAD_URI = "http://losiek900.cba.pl/upload/upload.php?filename={0}&append={1}";
/// <summary>
/// Filter for the file upload dialog
/// </summary>
public const string UPLOAD_DIALOG_FILTER = "All files (*.*)|*.*|Jpeg Images (*.jpg)|*.png|PNG Images (*.png)|*.png";
private Stream _data;
private string _fileName;
private long _bytesTotal;
private long _bytesUploaded;
public MainPage()
{
InitializeComponent();
progressBar.Visibility = Visibility.Collapsed;
textBox.Visibility = Visibility.Collapsed;
}
private void Upload_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Multiselect = false;
dlg.Filter = UPLOAD_DIALOG_FILTER;
bool? retVal = dlg.ShowDialog();
if (retVal != null && retVal==true)
{
progressBar.Visibility = Visibility.Visible;
textBox.Visibility = Visibility.Visible;
textBox.Text = "Upload starting...";
_data = dlg.File.OpenRead();
_bytesTotal = _data.Length;
_bytesUploaded = 0;
_fileName = dlg.File.Name;
progressBar.Maximum = _bytesTotal;
UploadFileChunk();
}
}
private void UploadFileChunk()
{
textBox.Text = "Upload in progress...";
string uploadUri = "";
if (_bytesUploaded == 0)
{
uploadUri = String.Format(UPLOAD_URI,_fileName,0); // Dont't append
}
else if (_bytesUploaded < _bytesTotal)
{
uploadUri = String.Format(UPLOAD_URI, _fileName, 1); // append
}
else
{
return; // Upload finished
}
byte[] fileContent = new byte[CHUNK_SIZE];
int bytesRead = _data.Read(fileContent, 0, CHUNK_SIZE);
_data.Flush();
WebClient wc = new WebClient();
wc.OpenWriteCompleted += new OpenWriteCompletedEventHandler(wc_OpenWriteCompleted);
Uri u = new Uri(uploadUri);
wc.OpenWriteAsync(u, null, new object[] { fileContent, bytesRead });
_bytesUploaded += fileContent.Length;
// MessageBox.Show(uploadUri);
}
void wc_OpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e)
{
progressBar.Value = _bytesUploaded;
if (e.Error == null)
{
object[] objArr = e.UserState as object[];
byte[] fileContent = objArr[0] as byte[];
int bytesRead = Convert.ToInt32(objArr[1]);
Stream outputStream = e.Result;
outputStream.Write(fileContent, 0, bytesRead);
outputStream.Close();
if (_bytesUploaded < _bytesTotal)
{
UploadFileChunk();
}
else
{
textBox.Text = "Upload complete!";
}
}
}
}