Based on a tutorial I've build an iOS app that uploads images from the device to an web server directory. It worked fine in my localhost, but when I uploaded the files to the server and tested the file weren't copied to the directory I specified! So I suppose in Xcode everything working correctly, the PHP file I using to upload to my server is bellow:
<?php
$connect = mysql_connect("xxxxxxxxx","xxxx","xxxx") or die(mysql_error());
$connectDB = mysql_select_db("xxxxxxxxx");
$insertData = "INSERT INTO feras (nome,vulgo,imagelink) VALUES ('".$_GET['feraname']."','".$_GET['feravulgo']."','http://localhost:8888/ImageUpIPhone/uploads/".$_GET['imagelink'].".jpg')";
$sendData = mysql_query($insertData);
//$file_name = substr ( md5(uniqid(rand(),1)), 5, 15);
$file_name = $_GET['imagelink'];
$uploaddir = 'uploads/';
$file = basename($_FILES['userfile']['name']);
$newname = $file_name . $file;
$uploadfile = $uploaddir . $newname;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
$imagepath = "http://example.com/uploads/{$newname}";
}
print $imagepath;
?>
So, how can I fix that? In my localhost it worked and in the server it doesn't, I can only figure that's about the directory! I using a free host (ueuo.com).
Related
Using the below php if I upload a .txt file named "example" the resulting uploaded file looks like
example.txt04-27-2020-1228
it is still has the extension .txt (which I want) but I am trying to get the file to read like this
example.04-27-2020-1228
Here is the code
<?php
$uploaddir = 'C:/xampp/htdocs/';
$uploadfile = $uploaddir . basename($_FILES['name']);
$withoutextension = substr($uploadfile, 0, strrpos($uploadfile, ".txt"));
$fileD = $uploaddir . $withoutextension.date('m-d-Y-Hi').".txt";
(move_uploaded_file($_FILES['file']['tmp_name'], $fileD))
?>
Any takers? Cheers.
I'm attempting to move an uploaded image (from Android) that is to be renamed via the PHP below in the second example so that their names cannot conflict. The original example below uploads files correctly but can have naming conflicts. The error that I'm experiencing is that the move_uploaded_files function fails, which I'm unsure as to why. The directory appears the same but I could be wrong and the problem is that image is never moved from the temp directory. Above all else, I think this is just a directory issue since the original example works. Please let me know if you need more information. The example I'm going by is located here: How to rename uploaded file before saving it into a directory?
Original:
$uploaddir = './appphotos/';
$absPath = 'https://'.$_SERVER['HTTP_HOST'].'/complaint_desk/appphotos/';
$file = basename($_FILES['userfile']['name']);
$uploadFile = $file;
$newName = $uploaddir . $uploadFile;
New Attempt:
$temp = explode(".",$_FILES["userfile"]["name"]);
echo json_encode($temp);
$newfilename = rand(1,99999) . '.' .end($temp);
echo json_encode($newfilename);
$uploadFile = move_uploaded_file($_FILES["userfile"]["name"], "/var/www/complaint_desk/appphotos/" . $newfilename); echo json_encode($uploadFile);
You should use the function as follow:
if(move_uploaded_file($_FILES["userfile"]["tmp_name"], "./appphotos/" . $newfilename)) {
echo json_encode($uploadFile); // why do you want to encode it?
} else {
echo 'File failed to move';
}
Always check the result of move_uploaded_file(). Also, the file is located at $_FILES["userfile"]["tmp_name"] before moving.
Also, $absPath is incorrect. It shouldn't start with http protocol. It should look like /var/www/complaint_desk/appphotos/ or C:/complaint_desk/appphotos/.
I have been trying to change the name of a file after an upload with my script.
I want every file to be named as "testing". Later I am going to change "testing" to a
variable that gets a unique name. Now I just want to change the name to "testing".
Also the script always says error although the file are uploaded.
Can I get some help here please?
Here is my code:
<?php
$uploadDir = 'images/'; //Image Upload Folder
if(isset($_POST['Submit']))
{
$fileName = $_FILES['Photo']['name'];
$tmpName = $_FILES['Photo']['tmp_name'];
$fileSize = $_FILES['Photo']['size'];
$fileType = $_FILES['Photo']['type'];
$filePath = $uploadDir . $fileName;
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$query = "INSERT INTO $db_table ( Image ) VALUES ('$filePath')";
mysql_query($query) or die('Error, query failed');
}
?>
I think you need
$fileName = "testing"; //maybe add extension
instead of getting original filename from $_FILES. Although after the file is moved you may end up with a situation of overwriting existing files as they all has the same name. To prevent that (for testing purposes) you may add something to $fileName, maybe a short random string.
I am new with php web service and trying upload an image using php web service from objective C. Actually, I have to upload an image on server and save its url in mySQL database. But I am unable to do that. Below is my code :
$file_name = substr ( md5(uniqid(rand(),1)), 5, 15);
$uploaddir = 'uploading/';
$file = basename($_FILES['userfile']['name']);
$newname = $file_name . $file;
$uploadfile = $uploaddir . $newname;
$name=$_GET['username'];
$email=$_GET['email'];
$pwd=$_GET['password'];
$phone=$_GET['phone'];
$fb_user=$_GET['facebook_user'];
$fb_pwd=$_GET['facebook_pwd'];
$twitter_user=$_GET['twitter_user'];
$twitter_pwd=$_GET['twitter_pwd'];
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
{
$query="INSERT INTO `user_master` (`name`,`email`,`password`,`phone`,`facebook_user`,`facebook_pwd`,`twitter_user`,`twitter_pwd`,`followers_no`,`following_no`,`profile_view_no`,`profile_photo`) VALUES ('$name','$email','$pwd','$phone','$fb_user','$fb_pwd','$twitter_user','$twitter_pwd',0,0,0,'$uploadfile')";
if(!mysql_query($query)) die('Error : '.mysql_error());
$id= mysql_insert_id();
mysql_close();
echo json_encode(array("response_code"=>"1","response_message"=> "Successful Transaction","UserId"=>"$id"));
}
else
{
echo json_encode(array("response_code"=>"2","response_message"=> "Please try Again."));
return;
}
The result I am getting is "Plase try again". Any help will be appreciated.
Thanks in advance
I was wondering if someone with more experience than me could take a quick second to have a look over my php script for uploading a file to my server.
I had a simple php script that uploaded my image the root of my server when I called the script in my code like so:
http://server.foo.com/images/uploadToDirectory.php
Now I'm trying to amend it so that I can put the name of a folder at the end with the following call:
http://server.foo.com/images/uploadToDirectory.php?dir=test_folder
But for some reason my image is only getting sent to the root of the server. I've checked the logic of my c# code so I think it must be something to do with my php script. Could someone please have a look over it and tell me if I'm doing something silly with my code?
<?
$_SESSION['directory'] = $_POST['directory'];
$uploaddir = './'.$_SESSION['directory'];
$file = basename($_FILES['file']['name']);
$uploadfile = $uploaddir . $file;
print_r($_FILES);
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
echo "http://server.ip.address/images/{$file}";
}
else
{
echo "Didn't Work!!!!";
}
?>
Please note, I know this is probably a really bad way for me to go about doing what I want to do, but it's the way I've implemented it. My knowledge of PHP isn't very good.
For comparison here is the code to load to the root of the server:
<?
$uploaddir = './';
$file = basename($_FILES['file']['name']);
$uploadfile = $uploaddir . $file;
print_r($_FILES);
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
echo "http://server.ip.address/images/{$file}";
}
else
{
echo "Didn't Work!!!!";
}
?>
Ok so.. if you invoke your script like this:
http://server.foo.com/images/uploadToDirectory.php?dir=test_folder
Then your script should be:
$uploaddir = './'.$_GET["dir"];
That would collect your URL GET variable "dir".