Hey, I have a site on a cluster, and I'm meant to upload everything to a certain IP and not the main ftp.domain.com part. This causes a problem as images end up going 404. The code I use is, $ext = strtolower(substr($file['name'], strrpos($file['name'], '.')+1)); if (move_uploaded_file($file['tmp_name'], $script_dir . "thumbs/" . $typestr . "_layout_" . $alias . "." . $ext)) Thats the code that stores the image file into the thumbs/ dir but not to the IP server. I need it to store it to an FTP server, in the /home/public_html/main/layouts/thumbs/ directory If that makes sense? Is this possible?
I guess this is not possible, even if the "/home/public_html/main/layouts/thumbs/ " directory have 777 permission it will not be possible to write files from remote location over HTTP protocol. you may try and using ftp function for writing/uploading files for ftp function refrence you can try here
So it is possible using FTP over PHP? I could have a go at working that out I'm just puzzled to what the file would be from this line if (move_uploaded_file($file['tmp_name'], $script_dir . "thumbs/" . $typestr . "_layout_" . $alias . "." . $ext)) ??
yeah you could, but you would still need to upload the file to a temporary location before you could move it via ftp functions, you want an example of that ???
<? define("FTP_SERVER", 'ftp.yoursite.com' ); define("FTP_USERNAME", 'username' ); define("FTP_PASSWORD", 'password' ); define("FTP_PATH", 'www/thumbs' ); define("FILES_TEMP", 'temp'); if( $_POST ) { if (move_uploaded_file($_FILES['upload']['tmp_name'], sprintf( '%s/%s', FILES_TEMP, basename($_FILES['upload']['name']) ) )) { printf( '%s was uploaded commencing ftp operation<br />', basename($_FILES['upload']['name']) ); if( ( $ftp = ftp_connect( FTP_SERVER ) ) ) { printf('Connected to %s<br />', FTP_SERVER ); if( ftp_login( $ftp, FTP_USERNAME, FTP_PASSWORD ) ) { printf('Logged into %s as %s<br />', FTP_SERVER, FTP_USERNAME ); if( ftp_put( $ftp, sprintf( '%s/%s', FTP_PATH, basename($_FILES['upload']['name']) ), sprintf( '%s/%s', FILES_TEMP, basename($_FILES['upload']['name']) ), FTP_BINARY ) ) { printf( '%s uploaded to %s<br />', basename($_FILES['upload']['name']), sprintf( '%s/%s', FTP_PATH, basename($_FILES['upload']['name']) ) ); if( unlink( sprintf( '%s/%s', FILES_TEMP, basename($_FILES['upload']['name']) ) ) ) { printf( 'Removed %s from temp directory<br />', basename($_FILES['upload']['name']) ); } else { printf( 'Cannot remove %s from temp directory<br />', basename($_FILES['upload']['name']) ); } } } else { printf( 'Cannot login to %s as %s<br />', FTP_SERVER, FTP_USERNAME ); } } else { printf( 'Cannot connect to %s<br />', FTP_SERVER ); } } else { print( 'Cannot upload file, errors occured, you should handle these yourself!' ); } } ?> <form enctype="multipart/form-data" action="" method="POST"> <input type="hidden" name="MAX_FILE_SIZE" value="800000" /> Choose a file : <input name="upload" type="file" /> <input type="submit" value="Upload" /> </form> PHP: There ya go, check the defines at the top, FILES_TEMP needs to be chmod'ed to 777 and the FTP_PATH must exist on the server, FTP_PATH is relative to your FTP root NOT http root, and you should not include trailing slashes on paths. Have fun.......
Hey, I'm having troubles implementing it into this, I just spent a couple hours and now I've decided to try again... This is the code that sorts the thumbnail, as its already in a premade script, <? $submit = $_POST['submit']; $name = $_POST['name']; $description = $_POST['description']; $type = $_POST['type']; $typestr = $_POST['typestr']; $category = $_POST['category']; if (!isset($_COOKIE['mys_user'])) { exit; } if (!isset($_COOKIE['mys_pass'])) { exit; } function getname($thename) { global $mysql_server,$mysql_user,$mysql_pass,$mysql_db; mysql_connect($mysql_server,$mysql_user,$mysql_pass); mysql_select_db($mysql_db); $tmpquery = mysql_query("SELECT * FROM `approved` WHERE LOWER(`name`) = '".strtolower($thename)."'"); $tmpresult = @mysql_fetch_array($query); mysql_close(); return empty($tmpresult); } if (isset($submit)) { if ($name == "") { echo "<b>Your template must have a name!" ; exit ; } if ($description == "") { echo "<b>Your template must have a description!" ; exit ; } // if (empty($file)) { echo "<b>You must upload a 150x150 thumbnail with your code!" ; exit ; } $count = 1; $old_name = $name; $alias = ereg_replace("[^[:space:]a-zA-Z0-9*_.-]", "", $name); $alias = str_replace(" ","-",strtolower($alias)); $typestr = ereg_replace("[^[:space:]a-zA-Z0-9*_.-]", "", $typestr); $typestr = str_replace(" ","-",strtolower($typestr)); foreach ($_FILES as $file) { if (array_search(strtolower(substr($file['name'], strrpos($file['name'], '.')+1)), $config['ftypes']) === false) { echo "<B>Invalid image file type!</B>"; exit; } $ext = strtolower(substr($file['name'], strrpos($file['name'], '.')+1)); if (move_uploaded_file($file['tmp_name'], $script_dir . "thumbs/" . $typestr . "_layout_" . $alias . "." . $ext)) { mysql_connect($mysql_server,$mysql_user,$mysql_pass); mysql_select_db($mysql_db); $time = time(); mysql_query("INSERT INTO `pending` VALUES('".$time."','".addslashes($name)."','".addslashes($description)."','".$type."','".$category."','".strtolower($_COOKIE['mys_user'])."','".$alias."','". $typestr . "_layout_" . $alias . "." . $ext."')"); $code[0] = ""; $temp = 1; while ($temp <= 10) { if (isset($_POST['code'.$temp])) { $code[$temp] = addslashes($_POST['code'.$temp]); } else { $code[$temp] = ""; } $temp = $temp + 1; } mysql_query("INSERT INTO `codes` VALUES('".$time."','".$code[1]."','".$code[2]."','".$code[3]."','".$code[4]."','".$code[5]."','".$code[6]."','".$code[7]."','".$code[8]."','".$code[9]."','".$code[10]."')"); mysql_close(); $to = $from_email; $subject = 'New layout pending - '.$name; $message = 'There is a new layout pending. Please check admin panel to approve or deny.'; mail($to, $subject, $message); } else { echo '<b>Thumbnail upload failed! Please contact site admin.</b><br>'; } } echo "<B>Your layout addition is currently pending. Note all pending layouts will not be published on our site until admin approval.</B><BR><BR>"; } ?> PHP: So that needs to be modified to send the file to the FTP server. It basically needs to keep the same image filename as the current snippet gives it so the rest of the script doesn't have a fit.