Php Image Upload
Moderator: General Moderators
Php Image Upload
Hi i have been trying to write a script to uplaod an image file into my server. But i have found out that i dont have permission to do this
i have been using the $_FILES method. Is there any other methods that wont be blocked? would i have to put it into a database? thanks!
Re: Php Image Upload
cm_oj wrote: i have found out that i dont have permission to do this
specifically, what makes you think this?
consider posting your code, maybe your just making an error.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
there are several ways to go about it, if your script really doesn't have permission to copy/move a file:
- manually create a copy of the file. doubtful it'd work if your permissions deny the former..
- have your script FTP them to where you want. This takes a while to set up, but generally gets around the problem of not having permission. Hopefully ftp is enabled.
- store them in the database. Very much not recommended as it eats up a lot of database, and you can only do 1 query per image. Plus you'll need enough php memory to transfer over the data.
I have full access to my website, but i want to make an image upload feature for my forum members. The forum is all my own script. I know file upload is blocked as i tried it, it worked on my localhost but when i did it online it said this :
Warning: Unable to create '/web/sites/305/cellarmation/www.cellarmation.f2s.com/newname.jpg': Permission denied in /web/sites/305/cellarmation/www.cellarmation.f2s.com/image.php on line 14
Warning: Unable to move '/tmp/phpuKKy5b' to '/web/sites/305/cellarmation/www.cellarmation.f2s.com/newname.jpg' in /web/sites/305/cellarmation/www.cellarmation.f2s.com/image.php on line 14
Is there anyway i can still use php, but not using the $_FILES method? thanks
Warning: Unable to create '/web/sites/305/cellarmation/www.cellarmation.f2s.com/newname.jpg': Permission denied in /web/sites/305/cellarmation/www.cellarmation.f2s.com/image.php on line 14
Warning: Unable to move '/tmp/phpuKKy5b' to '/web/sites/305/cellarmation/www.cellarmation.f2s.com/newname.jpg' in /web/sites/305/cellarmation/www.cellarmation.f2s.com/image.php on line 14
Is there anyway i can still use php, but not using the $_FILES method? thanks
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
only route I see, if the others are truely exhausted, is sending them into the database... which is a giant pain.
so lets get this straight: you've tried [php_man]move_uploaded_file()[/php_man] which failed. you tried manually creating the copy ([php_man]fopen()[/php_man]) ? Did you try the FTP route? If yes to all those, then through the database it is.. Or, see about getting the script or php write access to some deeper level folder.
so lets get this straight: you've tried [php_man]move_uploaded_file()[/php_man] which failed. you tried manually creating the copy ([php_man]fopen()[/php_man]) ? Did you try the FTP route? If yes to all those, then through the database it is.. Or, see about getting the script or php write access to some deeper level folder.
I am not sure if i have used those to methods, i think move_uploaded_file has been used in one part of the script. I was ripped of a tutorial, and modified, i know it works on y localhost :
how does fopen() work? can that be used to upload a file to a remote server? thank you!
Code: Select all
<?php
//Define some variables
$dir = $_SERVER['DOCUMENT_ROOT']; //Change this to the correct dir
//MIME types to allow, Gif, jpeg, zip ::Edit this to your liking
$types = array("image/gif","image/jpeg");
//Check to determine if the submit button has been pressed
if(isset($_POST['submit'])){
//Shorten Variables
$tmp_name = $_FILES['upload']['tmp_name'];
$new_name = $_FILES['upload']['name'];
//Check MIME Type
if (in_array($_FILES['upload']['type'], $types)){
//Move file from tmp dir to new location
move_uploaded_file($tmp_name,$dir . "/newname.jpg");
echo " was uploaded sucessfully!";
}else{
//Print Error Message
echo "<small>File <strong><em></em></strong> Was Not Uploaded!</small><br />";
//Debug
$name = $_FILES['upload']['name'];
$type = $_FILES['upload']['type'];
$size = $_FILES['upload']['size'];
$tmp = $_FILES['upload']['name'];
echo "Name: $name<br/ >Type: $typ<br />Size: $size<br />Tmp: $tmp";
}
}
else{
echo 'Could Not Upload Files';
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<fieldset>
<legend>Upload Files</legend>
<input type="file" name="upload" />
</fieldset>
<input type="submit" name="submit" value="Upload Files" />
</form>- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
fopen is not able to write to a remote server. fopen can create and/or open a file on the local system. You may just get a blanket denial to write to the document root. Try a directory off the root. If you have access to the permissions of your own subfolders you may be able to get that folder writable. May want to talk to your provider if you don't know what to do that.
But then surely my ftp password&username would be needed to be specified? could you show me how i could put that into my code? Would that mean i would have to have a lot more code? or would it be something completly differant like in the http://www.php.net link you put in?
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
the username and password are passed in as part of the connection opening sequence. Look through the example they have on that page:
Code: Select all
<?php
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}
// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
// close the FTP stream
ftp_close($conn_id);
?>- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact: