Uploading images from Windows Phone 8 app to a website - php

I want to be able to upload images from my Windows Phone 8 app, to my website.
For this, I followed the tutorial from this website:
https://vortexwolf.wordpress.com/2013/06/04/windows-phone-select-and-upload-image-to-a-website-over-http-post/
It all worked good, on the windows phone app side. But I have problems getting the website upload.php file working. In that tutorial, the author is using http://posttestserver.com/post.php?dir=wp7posttest to get the response of upload. In my case, using that URL for testing was working good, but when I put my website url, I get no response, and a crash in visual studio, with following error:
Additional information: The remote server returned an error: NotFound.
This error happens is thrown on this line:
response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
The upload.php on my website, looks like this:
if(isset($_GET['dir']))
{
$dir = $_GET['dir'];
if($_FILES['photo']['name'])
{
if(!$_FILES['photo']['error'])
{
$new_file_name = strtolower($_FILES['photo']['tmp_name']);
if($_FILES['photo']['size'] > (1024000))
{
$valid_file = false;
$message = 'Oops! Your file\'s size is to large.';
}
if($valid_file)
{
move_uploaded_file($_FILES['photo']['tmp_name'], '../$dir/' . $new_file_name);
$message = 'Congratulations! Your file was accepted.';
}
}
else
{
$message = 'Ooops! Your upload triggered the following error: '.$_FILES['photo']['error'];
}
}
echo $message;
}
I am out of ideas, and stuck on this problem for the second day. Any answers/ideas of how to get the website upload.php file working properly?
Thanks!

Related

PHP copy() is only copying half the image

https://trevim.pt/header-4/
I'm copying this image to this link
https://trevim.pt/anuncios/header.png
When I run the script it always says 'File copied successfully' but some times I only get the top half of the image... it's not at all a large file (22.6KB), is this behavior normal? How can I copy the whole image every time? Any ideas? Here's my code:
foreach($to_activate as $data_row)
{
$template_id = $data_row['template_id'];
$img_url = $data_row['img_url'];
$template = mysqli_query($conn, "SELECT name FROM `wp_ad_templates` WHERE id = '$template_id' limit 1")->fetch_object()->name;
$dst = "../" . $template . ".png";
if(!#copy($img_url, $dst))
{
$errors= error_get_last();
echo "COPY ERROR: ".$errors['type'];
echo "<br>".$errors['message']."<br><br>";
} else {
echo "File copied from remote!";
}
}
By the way I found this similar question Php copy only copies part of file which is solved by using exec() but that solution didn't work for me, I get:
COPY ERROR: 2
exec() has been disabled for security reasons

CKEditor issue with drag and drop image upload

I'm having some difficulties configuring uploadimage plugin on CKeditor to permit drag and drop of images.
I included both plugins uploadimage and it's dependent uploadwidget.
This is the upload php file:
$basePath = "/var/www/html/images/articles/";
$baseUrl = "/var/www/html/images/articles/";
$funcNum = $_REQUEST['CKEditorFuncNum'] ;
if (isset($_FILES['upload'])) {
$name = $_FILES['upload']['name'];
move_uploaded_file($_FILES["upload"]["tmp_name"], $basePath . $name);
$url = $baseUrl . $name ;
$message = 'new file uploaded';
}
else
{
$message = 'No file has been sent';
}
echo "<script type='text/javascript'> window.parent.CKEDITOR.tools.callFunction($funcNum, '$url', '$message')</script>";
The problem is with CKEditorFuncNum, since I'm not receiving anything from the form.
File is correctly uploaded into the $basePath but the function window.parent.CKEDITOR.tools.callFunction fail due to the missing funcNum
ckeditor.js:3 [CKEDITOR] Error code: filetools-response-error.
Object {responseText: "<br />↵<b>Notice</b>: Undefined index: CKEditorFu…icles/371c63d.jpg', 'new file uploaded')</script>"}
Do you guys know why I'm not getting anything from $_REQUEST['CKEditorFuncNum'] ?
THanks
Have you checked docs on File Browser API describing creation of custom file manager?
As for CKEditorFuncNum, I would check if it is a problem with CKEditor or PHP code. Just check the upload request in the browser dev console to see if CKEditor adds CKEditorFuncNum param:
And then depending on the cause you may try to dive deeper.

Multiple file upload scripts have stopped working

I have a site with multiple file uploaders on it, all of which have stopped working.
I am getting no errors; it's like the PHP just refuses to acknowledge the $_FILES handler.
This only occurs on the live server; on my local development system the exact same files work fine.
The HTML I use is:
<input type="file" name="docfile">
The PHP is:
if ($_FILES['docfile']['name'] != '') {
if (!file_exists('upld/documents/'.$docid)) {
mkdir('upld/documents/'.$docid, 0777, true);
}
//sanitise filename
$cleanname = preg_replace('/\s+/', '-', $_FILES['docfile']['name']);
$upfile = "v0-" . $cleanname;
//Upload image
$temp = $_FILES['docfile']['tmp_name'];
$dest = "upld/documents/".$docid."/";
if(move_uploaded_file($temp,$dest.$upfile))
{
//save file name in DB MySQL stuff here
} else {
$errors[] = "There was an error while uploading file";
}
}
Could a change on the server have affected this?
It turned out that someone had run an update on the server which had reset the php.ini setting file_uploads to OFF.
Once I changed it back to ON everything worked again.

Uploading files using php very slow in xampp

I am new for developing.I have opted for php to learn coding.So I might make mistake as I learn by myself, kindly clarify my doubts.
I have problem in uploading files using php to a folder.What I really do is, I upload a file and the file is saved in a folder and the name of the file alone inserted in the database. While uploading the file I do copy the file to another folder which will be used for the editing purpose so that the original file will not be disturbed.Here the problem I get is, the file is uploaded successfully as well as the name too inserted in database. But it take much time to get upload even the size of the file is small.It works good while I test using my local but when I come in real time this issue(slow uploading) I face. What the person incharge in uploading do is, uploading a file and opening a new browser and upload another file. When the new browser is opened the files get uploaded but in the previous browser it is still in process. The code I have written to copy the file to another folder is not executed as the new browser is opened to upload another set of files. I am using xamp cp v3.2.1.To minimize the execution time I have set the default Maximum execution time to 30. But unable to upload file fastly.
Below is my php coding:
<?php
// connect to the database
include('connect-db.php');
if (isset($_POST['submit']))
{
// get form data, making sure it is valid
$udate = mysql_real_escape_string(htmlspecialchars($_POST['udate']));
$file_array=($_FILES['file_array']['name']);
// check to make sure both fields are entered
if ($udate == '' || $file_array=='')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
// if either field is blank, display the form again
renderForm($udate, $file_array, $error);
}
else
{
$udate = mysql_real_escape_string(htmlspecialchars($_POST['udate']));
if(isset($_FILES['file_array']))
{
$name_arrray=$_FILES['file_array']['name'];
$tmp_name_arrray=$_FILES['file_array']['tmp_name'];
for($i=0;$i <count($tmp_name_arrray); $i++)
{
if(move_uploaded_file($tmp_name_arrray[$i],"test_uploads/".str_replace(' ','',$name_arrray[$i])))
{
// save the data to the database
$j=str_replace(' ','',$name_arrray[$i]);
echo $j;
$udate = mysql_real_escape_string(htmlspecialchars($_POST['udate']));
$provider = mysql_real_escape_string(htmlspecialchars($_POST['provider']));
$existfile=mysql_query("select ubatch_file from batches");
while($existing = mysql_fetch_array( $existfile)) {
if($j==$existing['ubatch_file'])
echo' <script>
function myFunction() {
alert("file already exists");
}
</script>';
}
mysql_query("INSERT IGNORE batches SET udate='$udate', ubatch_file='$j',provider='$provider',privilege='$_SESSION[PRIVILEGE]'")
or die(mysql_error());
echo $name_arrray[$i]."uploaded completed"."<br>";
$src = 'test_uploads';
$dst = 'copy_test_uploads';
$files = glob("test_uploads/*.*");
foreach($files as $file){
$file_to_go = str_replace($src,$dst,$file);
copy($file, $file_to_go);
/* echo "<script type=\"text/javascript\">
alert(\"CSV File has been successfully Uploaded.\");
window.location = \"uploadbatches1.php\"
</script>";*/
}
} else
{
echo "move_uploaded_file function failed for".$name_array[$i]."<br>";
}
}
}
// once saved, redirect back to the view page
header("Location:uploadbatches1.php");
}
}
?>
It takes much time because, each and everytime all the files are copied to the newfolder. This exceeds the execution time.Only copying the uploaded files makes uploading and copying files fast.

Basic file upload - 500 Internal Server Error

Just playing around with uploading files as it's actually something I've never done before. I copied some supposedly working code from here.
I'm using cPanel hosting from Namecheap, with absolutely nothing changed from the default config.
I think the most likely problem is something very basic that I haven't activated. My HTML looks like this
<html>
<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data">
Your Photo: <input type="file" name="photo" size="25" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
and my PHP looks like this
<?php
//if they DID upload a file...
if($_FILES['photo']['name'])
{
//if no errors...
if(!$_FILES['photo']['error'])
{
//now is the time to modify the future file name and validate the file
$new_file_name = strtolower($_FILES['photo']['tmp_name']); //rename file
if($_FILES['photo']['size'] > (1024000)) //can't be larger than 1 MB
{
$valid_file = false;
$message = 'Oops! Your file\'s size is to large.';
}
//if the file has passed the test
if($valid_file)
{
//move it to where we want it to be
move_uploaded_file($_FILES['photo']['tmp_name'], 'uploads/'.$new_file_name);
$message = 'Congratulations! Your file was accepted.';
}
}
//if there is an error...
else
{
//set that to be the returned message
$message = 'Ooops! Your upload triggered the following error: '.$_FILES['photo']['error'];
}
}
//you get the following information for each file:
$_FILES['field_name']['name']
$_FILES['field_name']['size']
$_FILES['field_name']['type']
$_FILES['field_name']['tmp_name']
}
When I try to upload an image, I get a 500 Internal Server Error when I hit submit.
What am I missing?
Thanks
Get rid of the stuff at the bottom:
<?php
//if they DID upload a file...
if($_FILES['photo']['name'])
{
//if no errors...
if(!$_FILES['photo']['error'])
{
//now is the time to modify the future file name and validate the file
$new_file_name = strtolower($_FILES['photo']['tmp_name']); //rename file
if($_FILES['photo']['size'] > (1024000)) //can't be larger than 1 MB
{
$valid_file = false;
$message = 'Oops! Your file\'s size is to large.';
}
//if the file has passed the test
if($valid_file)
{
//move it to where we want it to be
move_uploaded_file($_FILES['photo']['tmp_name'], 'uploads/'.$new_file_name);
$message = 'Congratulations! Your file was accepted.';
}
}
//if there is an error...
else
{
//set that to be the returned message
$message = 'Ooops! Your upload triggered the following error: '.$_FILES['photo']['error'];
}
}
Not sure what that was for... Also, try checking the Namecheap php.ini in your CPanel to see what the max upload size is so your users get your error, not a PHP error or a 500.

Categories