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.
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.jpginstead.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 meanc:/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.