Reputation: 355
The php file for the form is:
<?php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 60000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("http://example.me/imgs/examples" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"http://example.me/imgs/examples" . $_FILES["file"]["name"]);
echo "Stored in: " . "http://example.me/imgs/examples" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
This is straight forward enough, right? The html for the form is:
<form action="img_upload.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<input type="submit" name="submit" value="Submit"/>
</form>
Also simple enough; correct? After submitting the image I get redirected to the img_upload.php file with the following text displayed:
Upload: example.jpg
Type: image/jpeg
Size: 39.1943359375 Kb
Temp file: C:\WINDOWS\Temp\php66.tmp
Stored in: http://example.me/imgs/examplesexample.jpg
I'm getting frustrated like mad when I attempt to do simple tasks like this and no errors but no results either. I've already attempted adding "/" like imgs/examples/ within the php script and the only difference that it made was the following line after submitting is:
Stored in: http://example.me/imgs/examples/example.jpg
Any ideas as to what I'm missing here. I have many other php scripts/mysql tables where images are being used and uploaded but for this simple task nothing. I'm using php 5 on a apache server testing on localhost. Thanks to all in advance for any assistance.
Upvotes: 0
Views: 3897
Reputation: 4297
It's unlikely that you can move a file to http://example.me/imgs/examples/
. That's not a path on your local system.
You need to move it to a path like C:/path/to/web/root/filename.jpg
instead.
This is different than other places where the http protocol might work because you are writing a file, not reading a file.
Your PHP script can't really know that your http://example.me/imgs/examples/
directory is on the same server as itself; and it can't figure out that by that URL you actually mean c:/apache/htdocs/imgs/examples/
. (Imagine if you had Apache set up to map the above directory somewhere totally different- or your file system set up to actually link that directory somewhere else! PHP can't possibly figure it out, it's just too complicated.) While it might be possible in some situations to write to an HTTP protocol URL, it's not really clear what that means, and the people who wrote PHP didn't implement that. If it were written it would probably take the form of uploading the file to that URL as a POST which would require you to write code to accept the file, and then move it somewhere on your local system... which is exactly what you're trying to do anyway.
Upvotes: 3
Reputation: 15
you must use a directory location system for upload. firstyl; don't use a http:// ! secondary: it can be located some directory for example; we want upload uploads/ dir on our .php file path
move_uploaded_file($_FILES["file"]["tmp_name"],"uploads/" . $_FILES["file"]["name"]);
move_uploaded_file($_FILES["file"]["tmp_name"],"../uploads/" . $_FILES["file"]["name"]);
Upvotes: 1
Reputation: 64526
The first problem is you are passing a URL to move_uploaded_file()
. That needs to be a local file system path instead. It's the same with file_exists()
, it can't take a URL. Example:
// if img directory is on the same level as the PHP script...
move_uploaded_file($_FILES["file"]["tmp_name"], "imgs/examples/" . $_FILES["file"]["name"]);
The second problem is you don't check the return value, you simply assume the move worked. move_uploaded_file()
returns a boolean true
on success, or false
on failure.
Try:
if(move_uploaded_file($_FILES["file"]["tmp_name"], "imgs/examples/" . $_FILES["file"]["name"]))
{
// worked
}
else
{
// didn't work
}
Upvotes: 2
Reputation: 17910
You are trying move your uploaded file using HTTP path (http://example.me/imgs/examples
) which won't work.
Specify server path like /imgs/examples/
[use the correct path, I'm just assuming]
move_uploaded_file($_FILES["file"]["tmp_name"],
"/imgs/examples/" . $_FILES["file"]["name"]);
Also you may have to change below line from HTTP path to Server path,
if (file_exists("http://example.me/imgs/examples" . $_FILES["file"]["name"]))
Upvotes: 2